php 更换 在使用 SimpleXML 打印 XML 内容之前使用 <br>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15716987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:49:26  来源:igfitidea点击:

Replacing &#xD; with <br> before printing XML contents using SimpleXML

phpxmlreplacesimplexmlnewline

提问by danicotra

I know it's probably a very simple issue but still I didn't find a solution... Ok, I'll be brief: suppose to have a so structured XML file:

我知道这可能是一个非常简单的问题,但我仍然没有找到解决方案......好吧,我会很简短:假设有一个如此结构化的 XML 文件:

<root><item><text>blah blah blah&#xD;blah blah blah&#xD;blah blah blah&#xD;...</text></item></root>

My XML is obviously more complex but that's not important since my question is: how do I replace those &#xD;with, for instance, html <br>tags?

我的 XML 显然更复杂,但这并不重要,因为我的问题是:我如何&#xD;用例如 html<br>标签替换它们?

I'm using SimpleXML to read data from XML and tried with:

我正在使用 SimpleXML 从 XML 读取数据并尝试使用:

echo str_replace("&#xD;", "<br>", $message->text);

and even with:

甚至:

echo str_replace("\n", "<br>", $message->text);

but nothing...

但没什么……

I need to use SimpleXML for this.

我需要为此使用 SimpleXML。

回答by IMSoP

&#xD;represents the ASCII "carriage return" character (ASCII code 13, which is Din hexadecimal), sometimes written "\r", rather than the "linefeed" character, "\n"(which is ASCII 10, or Ain hex). Note that when SimpleXML is asked for the string content of a node (with (string)$nodeor implicitly with statements like echo $node) it will turn this "entity" into the actual character it represents.

&#xD;表示 ASCII“回车”字符(ASCII 码13D十六进制),有时写作"\r",而不是“换行”字符"\n"(ASCII10A十六进制)。请注意,当 SimpleXML 被要求提供节点的字符串内容(使用(string)$node或隐式使用类似 的语句echo $node)时,它会将这个“实体”转换为它所代表的实际字符。

Depending on your platform (Windows, Linux, MacOS, etc), the standard line-ending, accessible via the built-in constant PHP_EOL, will be either "\n", "\r\n", or "\r".

根据您的平台(Windows,Linux和MacOS的,等等),标准行结束,通过访问内置的固定上PHP_EOL,将是要么"\n""\r\n""\r"

The safest way to replace these with HTML linebreak tags (<br>) is to replace anyof these characters, since you don't know which convention the sourceof the XML data might have been using.

用 HTML 换行标记 ( <br>) 替换这些字符的最安全方法是替换这些字符中的任何一个,因为您不知道XML 数据可能使用了哪种约定。

PHP has a built-in function which should be able to do this for you, called nl2br(). If you want a slightly custom version, there's a comment in the docs from "ngkongs" showing how to use str_replaceto similar effect.

PHP 有一个内置函数,它应该能够为您执行此操作,称为nl2br(). 如果你想要一个稍微自定义的版本,“ngkongs”的文档中有一条评论,展示了如何使用str_replace类似的效果。

回答by danicotra

I figured it out how to solve just before posting my question so, having already written, I'll share this hoping it'll be useful for someone else sooner or later...

我在发布我的问题之前就想出了如何解决,所以,已经写了,我会分享这个,希望它迟早对其他人有用......

This does the trick:

这是诀窍:

echo str_replace(PHP_EOL, "<br>", $message->text);