简体   繁体   English

php编码问题htmlentities

[英]php encoding issue htmlentities

I have user input and use htmlentities() to convert all entities. 我有用户输入,并使用htmlentities()转换所有实体。 However, there seems to be some bug. 但是,似乎存在一些错误。 When I type in 当我输入

ääää öööö üüüü ääää

I get 我懂了

ääää öööö üüüü ääää

Which looks like this 看起来像这样

ääää öööö üüüü ääää ää¤Ã¤Ã¶Ã¶¶¶Ãüüüüääää¤

What am I doing wrong? 我究竟做错了什么? The code is really only this: 该代码实际上只是这样:

$post=htmlentities($post);

EDIT 1 编辑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);

You must manually specify the encoding ( UTF-8 ) for htmlentities() : 您必须手动为htmlentities()指定编码( UTF-8 htmlentities()

echo htmlentities("ääää öööö üüüü ääää", null, "UTF-8");

Output: 输出:

ääää öööö üüüü ääää

it is important that 3th parameter of htmlentities matches the character set that uses the post. htmlentities的第3个参数与使用帖子的字符集相匹配很重要。 I supouse, you are NOT submiting utf8, as it is the default in htmlentities 我想,您没有提交utf8,因为它是htmlentities中的默认设置

in PHP 在PHP中

 $post = htmlentities ( $post, ENT_COMPAT, 'ISO-8859-1')  // or whatever  

in Form 通知

 <form action="your.php" accept-charset="ISO-8859-1">

anyway, actualy I recommend you to use utf8 无论如何,实际上我建议您使用utf8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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