且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在xml节点中保留换行符

更新时间:2023-11-13 22:02:46

您会踢自己。 ’\n’应该是 \n !这是一个完整的示例:

You're going to kick yourself. '\n' should be "\n"! Here's a full example:

$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>';