如何在回显字符串时保持PHP不受strippng XML标记的影响

use*_*864 2 php xml php-5.6

我正在通过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>.
Run Code Online (Sandbox Code Playgroud)

但相反,我得到以下内容:

Missing test tag. Please add the test tag and set it to true. i.e. true.
Run Code Online (Sandbox Code Playgroud)

如您所见,"数据"和"测试"xml标签正在被剥离.

Mur*_*nik 5

你可以将它们转换为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);
Run Code Online (Sandbox Code Playgroud)