我想使用htmlspecialchars但只能转换更大(>)和更小(<)的符号。我不想转换单,双和'&'迹象。我尝试并搜索了很多,但找不到答案。请帮忙。
答案 0 :(得分:2)
$replace = array('<' => '<', '>' => '>');
$string=strtr($string, $replace);
此处讨论了str_replace和strtr之间的差异:When to use strtr vs str_replace?
答案 1 :(得分:0)
尝试使用str_replace()函数,将数组作为“haystack中的针”:
$search_for_lt = array("<","<");
$string = str_replace($search_for_lt, "replacement here, can be empty", $string);
$search_for_gt = array(">","<");
$string = str_replace($search_for_gt, "replacement here, can be empty", $string);
你能否确认它按预期工作?
答案 2 :(得分:0)
为什么不只使用str_replace替换那些字符呢?
$result = str_replace(['<', '>'], ['<', '>'], $string);