Php 在xml节点中保留换行符
我试图将XML节点中的行转换为无序列表,但遇到了一些困难 以该节点为例:Php 在xml节点中保留换行符,php,xml,simplexml,domdocument,Php,Xml,Simplexml,Domdocument,我试图将XML节点中的行转换为无序列表,但遇到了一些困难 以该节点为例: <test> Line1 Line2 Line3 </test> 我还尝试了explode,以获得包含每行元素的数组: $lines = explode( '\n', $node->nodeValue); 但是,它只返回一个包含一个元素的数组,所以我不能用它创建无序列表 有没有一个简单的方法让我做到这一点 谢谢。你要踢自己了“\n”应该是“\n”!
<test>
Line1
Line2
Line3
</test>
我还尝试了explode
,以获得包含每行元素的数组:
$lines = explode( '\n', $node->nodeValue);
但是,它只返回一个包含一个元素的数组,所以我不能用它创建无序列表
有没有一个简单的方法让我做到这一点
谢谢。你要踢自己了<代码>“\n”应该是
“\n”
!下面是一个完整的示例:
$Dom = new DOMDocument('1.0', 'utf-8');
$Dom->loadXML(
'<test>
Line1
Line2
Line3
</test>');
$value = $Dom->documentElement->nodeValue;
$lines = explode("\n", $value);
$lines = array_map('trim', $lines); // remove leading and trailing whitespace
$lines = array_filter($lines); // remove empty elements
echo '<ul>';
foreach($lines as $line) {
echo '<li>', htmlentities($line), '</li>';
}
echo '</ul>';
$Dom=新的DOMDocument('1.0','utf-8');
$Dom->loadXML(
'
第1行
第2行
第3行
');
$value=$Dom->documentElement->nodeValue;
$lines=分解(“\n”,$value);
$lines=数组_映射($trim',$lines);//删除前导和尾随空格
$lines=数组_过滤器($lines);//删除空元素
回声“”;
foreach($line作为$line){
回音“- ”,htmlentities($line),“
”;
}
回声“
”;
…你说得对,它很有效,谢谢。这么小的疏忽给我带来了这么多的麻烦,我现在要开始自责了。
$lines = explode( '\n', $node->nodeValue);
$Dom = new DOMDocument('1.0', 'utf-8');
$Dom->loadXML(
'<test>
Line1
Line2
Line3
</test>');
$value = $Dom->documentElement->nodeValue;
$lines = explode("\n", $value);
$lines = array_map('trim', $lines); // remove leading and trailing whitespace
$lines = array_filter($lines); // remove empty elements
echo '<ul>';
foreach($lines as $line) {
echo '<li>', htmlentities($line), '</li>';
}
echo '</ul>';