我有用户输入并使用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]|(?|\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,'\\0',$post);
//Keep line breaks (more than one will be stripped above)
$post=nl2br($post);
//Remove more than one linebreak
$post=preg_replace("/(
\s*)+/", "
", $post);
//Secure with mysql_real_escape_string()
$post=mysql_real_escape_string($post);
解决方法:
您必须手动指定htmlentities()的编码(UTF-8):
echo htmlentities("ääää öööö üüüü ääää", null, "UTF-8");
输出:
ääää öööö üüüü ääää
标签:php,encoding
来源: https://codeday.me/bug/20191007/1866764.html