htmlspecialchars 无法正常工作!我想要 < 成为 gt,而不是 >

htmlspecialchars is not working as I want! I want < to be gt, NOT &gt;

这是我的输入:

<

这是我需要的输出:

gt

但这是我从 htmlspecialchars 获得的输出:

&gt;

基本上,我想要 htmlspecialchars 结果,但实际上是精简的。我只想要字符 a-z0-9.

有人可以帮忙吗?

您可以使用正则表达式删除 &;:

$result = preg_replace('/&(\w+);/', '', htmlspecialchars($string));

这应该适合你:

(只是 preg_replace() all characters which aren't in a-z0-9 with an empty string which you get from htmlentities()

<?php

    $str = "<";
    echo preg_replace("/[^a-z0-9]/", "", htmlentities($str));

?>

输出:

lt