work in simplexml_load_file () var file I load xml in php via simplexml_load_file (). Uploading a file with this: $xml ...">

PHP: How to get <br/"> work in simplexml_load_file () var file

I load xml in php via simplexml_load_file ().

Uploading a file with this:

$xml = simplexml_load_file('flash/datas/datas.xml'); 

And access to my content:

 $descText = $xml->aboutModule->chocolaterie->desc 

The text from desc is well registered in my $ descText, but all <br/> text disappear ... So my long text is on one line, not very well: - /

Do you know how to solve this? Is there a special way to make one $xml var? Or something else?

Thank you in advance for your help!

+4
source share
4 answers

Two ways to make it work.

Correct

Do not save these <br/> . Keep the actual actual translation strings, for example.

 <desc> Line 1. Line 2. </desc> 

Then use nl2br () to replace newlines with HTML tags. This assumes that your description usually does not contain markup. If so, use CDATA partitions as suggested in another answer.

Other

 $descText = strip_tags($xml->aboutModule->chocolaterie->desc->asXML(), '<br /><br/>'); 
+4
source

Take a look at this one .

There is a way to return HTML tags. For instance:

 <?xml version="1.0"?> <intro> Welcome to <b>Example.com</b>! </intro> 

This is the PHP code:

 <?php // I use @ so that it doesn't spit out content of my XML in an // error message if the load fails. The content could be // passwords so this is just to be safe. $xml = @simplexml_load_file('content_intro.xml'); if ($xml) { // asXML() will keep the HTML tags but it will also keep the parent tag <intro> so I strip them out with a str_replace. You could obviously also use a preg_replace if you have lots of tags. $intro = str_replace(array('<intro>', '</intro>'), '', $xml->asXML()); } else { $error = "Could not load intro XML file."; } ?> 
+1
source

If you have control over the source XML, you might think that it is better to store this description as character data, that is, in a CDATA wrapper, and not mix your XML with HTML.

For example, instead of:

 ... <desc> This is a description<br /> with a break in it </desc> ... 

... do something like:

 ... <desc> <![CDATA[ This is a description<br /> with a break in it ]]> </desc> 

But if you haven’t done so, then, as Casidiablo says, you will need to grab the contents of the <desc> element as XML.

+1
source

Better you can use the following code to replace BR tags with new string characters.

preg_replace("/(\s*)+/", "\n", $input);

OR

you can convert the contents of the description to "htmlentities" and then return to "htmlspecialchars"

0
source

Source: https://habr.com/ru/post/1309320/


All Articles