Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:如何获取<;br/>;在simplexml_load_file()变量中工作_Php_Xml_Simplexml - Fatal编程技术网

PHP:如何获取<;br/>;在simplexml_load_file()变量中工作

PHP:如何获取<;br/>;在simplexml_load_file()变量中工作,php,xml,simplexml,Php,Xml,Simplexml,我通过simplexml\u load\u file()在php中加载xml 我加载带有以下内容的文件: $xml = simplexml_load_file('flash/datas/datas.xml'); 访问我的内容的方法如下: $descText = $xml->aboutModule->chocolaterie->desc desc中的文本在我的$descText中注册良好,但是所有的文本都消失了。。。所以我的长文本只有一行,不太好:-/ 你知道怎么解决这个问题

我通过simplexml\u load\u file()在php中加载xml

我加载带有以下内容的文件:

$xml = simplexml_load_file('flash/datas/datas.xml');
访问我的内容的方法如下:

$descText = $xml->aboutModule->chocolaterie->desc
desc中的文本在我的$descText中注册良好,但是所有的

文本都消失了。。。所以我的长文本只有一行,不太好:-/

你知道怎么解决这个问题吗?执行
$xml
var有什么特别的方法吗?还是别的什么

提前感谢您的帮助

看一看

有一种方法可以获取HTML标记。例如:


欢迎来到Example.com!
这是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.";
}
?>

如果您可以控制源XML,您可能会发现最好将描述存储为字符数据,即在CDATA包装中,而不是将XML与HTML混合

例如,而不是:

...
<desc>
    This is a description<br /> with a break in it
</desc>
...
。。。
这是一个带有中断的描述
...
…做一些类似于:

...
<desc>
<![CDATA[
    This is a description<br /> with a break in it
]]>
</desc>
。。。
休息一下
]]>

但是如果没有,那么正如Casidiablo所说,您必须将
元素的内容获取为XML

对的 不要存储那些

。存储真实的换行符,例如

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

第1行。
第2行。
然后使用nl2br()将换行符替换为HTML标记。这是假设您的描述通常不包含标记。如果是,请使用另一个答案中建议的CDATA部分

另一个
$descText=strip_标签($xml->aboutModule->chocolate->desc->asXML(),“

”);
您可以使用以下代码将BR标记替换为新行字符


preg_replace(“/(\s*)+/”、“\n”、$input);


您可以将描述内容转换为“htmlspecialchars”,然后返回到“htmlspecialchars”

推荐:阅读此:或此:并认识到,您的XHTML以以下内容开头:
xmlns=”http://www.w3.org/1999/xhtml“
太棒了!谢谢你的帮助!即使只使用->asXML()命令,它也可以完美地工作。我惊讶地发现,堆栈溢出不是任何城市传奇!这么快回答,非常感谢大家!是的,我完全同意将HTML与XML=bleh混合使用。这一个更好!非常感谢。
<desc>
    Line 1.
    Line 2.
</desc>
$descText = strip_tags($xml->aboutModule->chocolaterie->desc->asXML(), '<br /><br/>');