【发布时间】:2021-02-27 09:30:01
【问题描述】:
我想将文本区域的内容保存在 XML 中,但由于某种原因 PHP 在 XML 输出中添加了
:
<?
$products = simplexml_load_file('data/custom.xml');
$product = $products->addChild('product');
$product->addChild('description', nl2br($_POST['description']));
//format XML output, simplexml can't do this
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($products->asXML());
file_put_contents('data/custom.xml', $dom->saveXML());
?>
<textarea class="form-control" id="description" name="description" placeholder="Description" rows="4"></textarea>
我正在使用 nl2br() 函数,因为我想将换行符转换为 <br>,但为什么在输出中添加(或离开?)换行符 &#xD;?
示例输出:
<?xml version="1.0"?>
<products>
</product>
<product>
<description>mfgdgan<br />
1<br />
2<br />
3</description>
</product>
</products>
【问题讨论】:
-
这行不通,因为
<br>是一个标签。您需要使用CDATA。