![](/img/trans.png)
[英]Unicode Replacement Characters in the PHP htmlspecialchars function
[英]php - htmlspecialchars with unicode
$string = "Główny folder grafik<p>asd nc</p>";
echo htmlspecialchars($string);
在现场
Główny folder grafik<p>asd nc</p>
在本地
Główny folder grafik<p>asd nc</p>
什么是问题? 我希望在实时站点上运行时结果看起来像本地
htmlspecialchars()
接受额外的参数——第三个是字符集。
尝试指定第三个参数。
您需要向 htmlspecialchars() 函数添加额外的参数。 以下应该工作:
htmlspecialchars($string, ENT_QUOTES, "UTF-8");
您可能希望将一个可选参数传递给htmlspecialchars关于字符集,默认情况下为 ISO-8859-1。
如果您需要翻译所有具有关联命名实体的字符串,请改用htmlentities()
,该函数在所有方面都与htmlspecialchars()
相同,除了htmlentities()
,所有具有 HTML 字符实体等效项的字符都被翻译成这些实体。
但即使是htmlentities()
也不会对所有unicode 字符进行编码。 它编码它可以[所有的latin1],而其他的则通过(例如`Љ)。
此函数会查询 ansii 表以自定义包含/省略您想要/不想要的字符。
(注意:肯定没有那么快)
/**
* Unicode-proof htmlentities.
* Returns 'normal' chars as chars and weirdos as numeric html entites.
* @param string $str input string
* @return string encoded output
*/
function superentities( $str ){
// get rid of existing entities else double-escape
$str = html_entity_decode(stripslashes($str),ENT_QUOTES,'UTF-8');
$ar = preg_split('/(?<!^)(?!$)/u', $str ); // return array of every multi-byte character
foreach ($ar as $c){
$o = ord($c);
if ( (strlen($c) > 1) || /* multi-byte [unicode] */
($o <32 || $o > 126) || /* <- control / latin weirdos -> */
($o >33 && $o < 40) ||/* quotes + ambersand */
($o >59 && $o < 63) /* html */
) {
// convert to numeric entity
$c = mb_encode_numericentity($c,array (0x0, 0xffff, 0, 0xffff), 'UTF-8');
}
$str2 .= $c;
}
return $str2;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.