Php只将引号转换为html代码,而不转换其他特殊字符


Php convert only quotes to html code but not other special chars

所以我的mysql中有'UAB "Litofcų kontora"'

当我试着把它放在像这样的输入中时

<input type="text" value="UAB "Litofc&#371; kontora"">因为引号而不显示全部内容如何使只有引号的代码替换为html代码?尝试了htmlentities和htmlspecialchar,但它将&#371;转换为,但我需要它不隐蔽。

在输出输入值之前,您必须(仅)用&quot;替换所有"。例如,带有str_replace:

$sInputValue = str_replace('"', '&quot;', $sValueFromDb);
echo '<input type="text" value="' . $sInputValue . '">';

另请参阅这个php-exp枫和由此产生的html示例。

您的问题似乎是,数据已被编码为HTML,但仅用作文本节点。

因此,解决方案是将它从HTML转换为文本,然后再转换回HTML——但要以适合放入属性的方式。

preg_replace_callback代码,因为html_entity_decode似乎不支持数字实体。

$input = 'UAB "Litofc&#371; kontora"';
$attribute_safe = htmlspecialchars(
        html_entity_decode(
            preg_replace_callback(
                "/(&#[0-9]+;)/",
                function($m) { return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES"); },
                $input
            )
        )
);
echo $attribute_safe;