如何在回显字符串时从 stripng XML 标签中保留 PHP

How to keep PHP from strippng XML tags when echoing a string

我正在通过 exec 在 PHP 中执行一个外部命令,我获取该输出(它是一个数组),然后让各个字符串搜索特定的字符串。然后我希望将这些字符串回显到屏幕上。但是,其中一些字符串包含 XML 个示例,并且它们正在被删除。如何防止 PHP 剥离 XML?我正在使用 PHP 5.6.2.

例如,我正在尝试回显具有以下输出的 $val:

Missing test tag.  Please add the test tag and set it to true.  i.e. <data><test>true</test></data>.

但是我得到了以下信息:

Missing test tag. Please add the test tag and set it to true. i.e. true.

如您所见,"data" 和 "test" xml 标签已被删除。

您可以将它们转换为 htmlentities:

$str = 'Missing test tag.  Please add the test tag and set it to true.  i.e. <data><test>true</test></data>.';
echo htmlentities($str, ENT_XHTML);