开发者

php: how to: echo "<input value='{$test}'>"; with $test=" (亜) (') (\") ";?

开发者 https://www.devze.com 2023-01-24 22:52 出处:网络
<?php $info = \" 亜 and \' and \\\" \"; $info=str_replace(array(\"\'\",\"\\\"\"),array(\"&#39;\",\"&#34;\"),$info);
<?php
$info = " 亜 and ' and \" ";
$info=str_replace(array("'","\""),array("&#39;","&#34;"),$info);
echo "<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>";
echo "<input id=info name=info value='{$info}' >";
?>开发者_如何学编程;

This works, BUT: Is there a tidier method like $info=htmlentities($info,ENT_QUOTES) which preserves the UTF-8 display? (htmlentities doesn't)


You need to specify third utf-8 parameter:

$info = htmlentities($info, ENT_QUOTES, 'UTF-8');

From Docs:

Like htmlspecialchars(), it takes an optional third argument charset which defines character set used in conversion. Presently, the ISO-8859-1 character set is used as the default.

Source: http://php.net/manual/en/function.htmlentities.php

0

精彩评论

暂无评论...
验证码 换一张
取 消