[英]php encoding issue htmlentities
我有用戶輸入,並使用htmlentities()轉換所有實體。 但是,似乎存在一些錯誤。 當我輸入
ääää öööö üüüü ääää
我懂了
ääää öööö üüüü ääää
看起來像這樣
ää¤Ã¤Ã¶Ã¶¶¶Ãüüüüääää¤
我究竟做錯了什么? 該代碼實際上只是這樣:
$post=htmlentities($post);
編輯1
這是我用於格式化的更多代碼(它們有一些有用的功能):
//Secure with htmlentities (mysql_real_escape_string() comes later)
$post=htmlentities($post);
//Strip obsolete white spaces
$post = preg_replace("/ +/", " ", $post);
//Detect links
$pattern_url='~(?>[a-z+]{2,}://|www\.)(?:[a-z0-9]+(?:\.[a-z0-9]+)?@)?(?:(?:[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])(?:\.[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])+|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?:/[^\\/:?*"<>|\n]*[a-z0-9])*/?(?:\?[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?(?:&[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?)*)?(?:#[a-z0-9_%.]+)?~i';
preg_match_all($pattern_url, $post, $matches);
for ($i=0; $i < count($matches[0]); $i++)
{
if(substr($matches[0][$i],0,4)=='www.')
$post = str_replace($matches[0][$i],'http://'.$matches[0][$i],$post);
}
$post = preg_replace($pattern_url,'<a target="_blank" href="\\0">\\0</a>',$post);
//Keep line breaks (more than one will be stripped above)
$post=nl2br($post);
//Remove more than one linebreak
$post=preg_replace("/(<br\s*\/?>\s*)+/", "<br/>", $post);
//Secure with mysql_real_escape_string()
$post=mysql_real_escape_string($post);
您必須手動為htmlentities()
指定編碼( UTF-8
htmlentities()
:
echo htmlentities("ääää öööö üüüü ääää", null, "UTF-8");
輸出:
ääää öööö üüüü ääää
htmlentities的第3個參數與使用帖子的字符集相匹配很重要。 我想,您沒有提交utf8,因為它是htmlentities中的默認設置
在PHP中
$post = htmlentities ( $post, ENT_COMPAT, 'ISO-8859-1') // or whatever
通知
<form action="your.php" accept-charset="ISO-8859-1">
無論如何,實際上我建議您使用utf8
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.