php:如何:echo“<input value =”{$ test}“/>”;与$ test =“(亜)(')(\”)“;?

时间:2010-11-10 13:45:22

标签: php html utf-8 quotes

<?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}' >";
?>

这有效,但是:是否有一种比$info=htmlentities($info,ENT_QUOTES)更整洁的方法保留 UTF-8 显示? (htmlentities

1 个答案:

答案 0 :(得分:8)

您需要指定第三个utf-8参数:

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

来自文档:

  

与htmlspecialchars()一样,需要一个   可选的第三个参数charset   定义中使用的字符集   转换。目前,ISO-8859-1   字符集用作默认值。

来源: http://php.net/manual/en/function.htmlentities.php