将特殊字符 '&' 、 '>' 和 '<' 转换为它们各自的 XML 字符引用
Convert special character '&' , '>' and '<' to their respective character reference for XML
我只需要为我的 XML 替换这 3 个符号:
- & 至
&
- < 至
<
- > 至
>
我使用了 htmlspecialchars 但它也会替换单引号和双引号。
查看 htmlspecialchars 文档。您可以使用标志来忽略引号(您可能对 ENT_NOQUOTES 标志感兴趣)。
这是同一篇文章中的示例:
<?php
$str = "Jane & 'Tarzan'";
echo htmlspecialchars($str, ENT_COMPAT); // Will only convert double quotes
echo "<br>";
echo htmlspecialchars($str, ENT_QUOTES); // Converts double and single quotes
echo "<br>";
echo htmlspecialchars($str, ENT_NOQUOTES); // Does not convert any quotes
?>
我只需要为我的 XML 替换这 3 个符号:
- & 至
&
- < 至
<
- > 至
>
我使用了 htmlspecialchars 但它也会替换单引号和双引号。
查看 htmlspecialchars 文档。您可以使用标志来忽略引号(您可能对 ENT_NOQUOTES 标志感兴趣)。
这是同一篇文章中的示例:
<?php
$str = "Jane & 'Tarzan'";
echo htmlspecialchars($str, ENT_COMPAT); // Will only convert double quotes
echo "<br>";
echo htmlspecialchars($str, ENT_QUOTES); // Converts double and single quotes
echo "<br>";
echo htmlspecialchars($str, ENT_NOQUOTES); // Does not convert any quotes
?>