简体   繁体   中英

php encoding issue htmlentities

I have user input and use htmlentities() to convert all entities. 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

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() :

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

Output:

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

it is important that 3th parameter of htmlentities matches the character set that uses the post. I supouse, you are NOT submiting utf8, as it is the default in htmlentities

in 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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