繁体   English   中英

php编码问题htmlentities

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM