0

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

当我尝试将它放入这样的输入时

<input type="text" value="UAB "Litofc&#371; kontora"">由于引号,它不会显示整个内容如何使仅引号替换为 html 代码?尝试了 htmlentities 和 htmlspecialchars 但它转换&#371;为但我需要它是不隐蔽的方式。

4

2 回答 2

0

在输出输入值之前,您必须(仅)将 all 替换"为。例如使用str_replace&quot;

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

另请参阅此 php示例和生成的 html 示例

于 2012-07-23T08:01:24.103 回答
0

看起来您的问题是数据已针对 HTML 进行编码,但仅用作文本节点。

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

preg_replace_callbackPHP手册中此注释中的代码,因为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;
于 2012-07-23T08:17:59.947 回答