PHP 正在添加不需要的 在输出中
PHP is adding unwanted 
 in the output
我想在 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>
,但为什么它在输出中添加(或离开?)换行符 
?
示例输出:
<?xml version="1.0"?>
<products>
</product>
<product>
<description>mfgdgan<br />
1<br />
2<br />
3</description>
</product>
</products>
简单XML元素不能直接附加xml标签。这意味着,您不需要的标签只是编码符号。但是在 DOM 系列函数中可以直接附加 xml 标签。值得庆幸的是,在同一个 XML 文档上同时使用 SimpleXML 和 DOM 很容易。
下面的示例使用 文档片段 向文档添加几个元素。
$products = simplexml_load_file('data/custom.xml');
$product = $products->addChild('product');
$description = $product->addChild('description');
$dom = dom_import_simplexml($description);
$fragment = $dom->ownerDocument->createDocumentFragment();
$fragment->appendXML(nl2br($_POST['description']));
$dom->appendChild($fragment);
echo $description->asXML();
P.S。我没有 运行 这段代码,可能有一些错误。只是一个解决问题的方向
\
是回车return,不是换行
无论如何,nl2br()
函数 插入 换行符 在字符串中 换行符的前面;它不会取代它们。
尝试使用:
str_replace(array("\r\n", "\r", "\n"), "<br/>")
或类似的东西而不是 nl2br()
。
我想在 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>
,但为什么它在输出中添加(或离开?)换行符 
?
示例输出:
<?xml version="1.0"?>
<products>
</product>
<product>
<description>mfgdgan<br />
1<br />
2<br />
3</description>
</product>
</products>
简单XML元素不能直接附加xml标签。这意味着,您不需要的标签只是编码符号。但是在 DOM 系列函数中可以直接附加 xml 标签。值得庆幸的是,在同一个 XML 文档上同时使用 SimpleXML 和 DOM 很容易。
下面的示例使用 文档片段 向文档添加几个元素。
$products = simplexml_load_file('data/custom.xml');
$product = $products->addChild('product');
$description = $product->addChild('description');
$dom = dom_import_simplexml($description);
$fragment = $dom->ownerDocument->createDocumentFragment();
$fragment->appendXML(nl2br($_POST['description']));
$dom->appendChild($fragment);
echo $description->asXML();
P.S。我没有 运行 这段代码,可能有一些错误。只是一个解决问题的方向
\
是回车return,不是换行
无论如何,nl2br()
函数 插入 换行符 在字符串中 换行符的前面;它不会取代它们。
尝试使用:
str_replace(array("\r\n", "\r", "\n"), "<br/>")
或类似的东西而不是 nl2br()
。