I am loading an xml in php via simplexml_load_file().
我通过simplexml_load_file()在php中加载一个xml。
I load the file with that:
我加载文件:
$xml = simplexml_load_file('flash/datas/datas.xml');
And the access my content like that:
并访问我的内容:
$descText = $xml->aboutModule->chocolaterie->desc
The text from desc is well registred in my $descText, but all the <br/>
of the text disappear... So my long text is on a single line, not so good :-/
来自desc的文本在我的$ descText中很好地注册了,但是文本的所有内容都消失了......所以我的长篇文章只有一行,不太好: - /
Do you know how to solve that? Is there a special traitement to do one the $xml
var? Or someting else?
你知道怎么解决这个问题吗?是否有一个特殊的traitement做一个$ xml var?或其他什么?
Thank you in advance for your help!
预先感谢您的帮助!
4 个解决方案
#1
4
Two ways to make it work.
两种方法使它工作。
The right one
Do not store those <br/>
. Store the real actual newlines, e.g.
不要存放那些。存储真实的实际换行符,例如
<desc>
Line 1.
Line 2.
</desc>
Then use nl2br() to replace newlines with HTML tags. This is assuming your description does not normally contain markup. If it does, use CDATA sections as proposed in another answer.
然后使用nl2br()用HTML标记替换换行符。这假设您的描述通常不包含标记。如果是,请使用另一个答案中提出的CDATA部分。
The other one
$descText = strip_tags($xml->aboutModule->chocolaterie->desc->asXML(), '<br /><br/>');
#2
1
Take a look of this.
看看这个。
There is a way to get back HTML tags. For example:
有一种方法可以取回HTML标签。例如:
<?xml version="1.0"?>
<intro>
Welcome to <b>Example.com</b>!
</intro>
This is the PHP code:
这是PHP代码:
<?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.";
}
?>
#3
1
If you have control over your source XML, you may find it better to store the description as character data, i.e. in a CDATA wrapper, rather than mixing your XML with HTML.
如果您可以控制源XML,您可能会发现将描述存储为字符数据(即在CDATA包装器中)更好,而不是将XML与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, then as Casidiablo says, you'll have to grab the content of the <desc>
element as XML.
但是如果你没有,那么正如Casidiablo所说,你必须将
#4
0
Better you can use the following code to replace the BR tags to new line chars.
更好的是,您可以使用以下代码将BR标记替换为新的行标记。
preg_replace("/(\s*)+/", "\n", $input);
preg_replace(“/(\ s *)+ /”,“\ n”,$ input);
OR
you can convert the description content to "htmlentities" and then turn back to "htmlspecialchars"
你可以将描述内容转换为“htmlentities”,然后转回“htmlspecialchars”
#1
4
Two ways to make it work.
两种方法使它工作。
The right one
Do not store those <br/>
. Store the real actual newlines, e.g.
不要存放那些。存储真实的实际换行符,例如
<desc>
Line 1.
Line 2.
</desc>
Then use nl2br() to replace newlines with HTML tags. This is assuming your description does not normally contain markup. If it does, use CDATA sections as proposed in another answer.
然后使用nl2br()用HTML标记替换换行符。这假设您的描述通常不包含标记。如果是,请使用另一个答案中提出的CDATA部分。
The other one
$descText = strip_tags($xml->aboutModule->chocolaterie->desc->asXML(), '<br /><br/>');
#2
1
Take a look of this.
看看这个。
There is a way to get back HTML tags. For example:
有一种方法可以取回HTML标签。例如:
<?xml version="1.0"?>
<intro>
Welcome to <b>Example.com</b>!
</intro>
This is the PHP code:
这是PHP代码:
<?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.";
}
?>
#3
1
If you have control over your source XML, you may find it better to store the description as character data, i.e. in a CDATA wrapper, rather than mixing your XML with HTML.
如果您可以控制源XML,您可能会发现将描述存储为字符数据(即在CDATA包装器中)更好,而不是将XML与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, then as Casidiablo says, you'll have to grab the content of the <desc>
element as XML.
但是如果你没有,那么正如Casidiablo所说,你必须将
#4
0
Better you can use the following code to replace the BR tags to new line chars.
更好的是,您可以使用以下代码将BR标记替换为新的行标记。
preg_replace("/(\s*)+/", "\n", $input);
preg_replace(“/(\ s *)+ /”,“\ n”,$ input);
OR
you can convert the description content to "htmlentities" and then turn back to "htmlspecialchars"
你可以将描述内容转换为“htmlentities”,然后转回“htmlspecialchars”