php编码问题htmlentities [英] php encoding issue htmlentities
本文介绍了php编码问题htmlentities的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有用户输入并使用htmlentities()来转换所有实体。
但是,似乎有一些bug。当我输入
I have user input and use htmlentities() to convert all entities. However, there seems to be some bug. When I type in
ääää öööö üüüü ääää
我得到
ääää öööö üüüü ääää
看起来像这样
à ¤Ã¤Ã¤Ã¤ Ã&段;Ã&段;Ã&段;Ã&段; üüüü ääää
ääää öööö üüüü ääää
我做错了什么?代码真的只有这样:
What am I doing wrong? The code is really only this:
$post=htmlentities($post);
编辑1
我用于格式化的代码(它们有一些有用的功能):
Here is some more code that I use for formatting purposes (there are some helpful functions it them):
//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);
推荐答案
您必须手动指定编码( UTF-8
) htmlentities ()
:
You must manually specify the encoding (UTF-8
) for htmlentities()
:
echo htmlentities("ääää öööö üüüü ääää", null, "UTF-8");
输出:
ääää öööö üüüü ääää
这篇关于php编码问题htmlentities的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文