htmlspecialchars仅转换更大(>)和更小(<)的符号

时间:2017-08-01 22:02:05

标签: php htmlspecialchars

我想使用htmlspecialchars但只能转换更大(>)和更小(<)的符号。我不想转换单,双和'&'迹象。我尝试并搜索了很多,但找不到答案。请帮忙。

3 个答案:

答案 0 :(得分:2)

$replace = array('<' => '&lt;', '>' => '&gt;');
$string=strtr($string, $replace);

此处讨论了str_replacestrtr之间的差异:When to use strtr vs str_replace?

答案 1 :(得分:0)

尝试使用str_replace()函数,将数组作为“haystack中的针”:

$search_for_lt = array("<","&lt;");
$string = str_replace($search_for_lt, "replacement here, can be empty", $string);
$search_for_gt = array(">","&lt;");
$string = str_replace($search_for_gt, "replacement here, can be empty", $string);

你能否确认它按预期工作?

答案 2 :(得分:0)

为什么不只使用str_replace替换那些字符呢?

$result = str_replace(['<', '>'], ['&lt;', '&gt;'], $string);