简体   繁体   中英

UTF-8 Character encoding trouble

I'm trying to implement this function to handle clean url's for my page: http://cubiq.org/the-perfect-php-clean-url-generator

It works fine for any character when I use it like this:

echo toAscii('åäö');

But as soon as I test it from an input (again with "åäö") form like this:

if (isset($_POST['test'])) {
    $test = $_POST['test'];
}
echo toAscii($test);

I get the following error: Notice: iconv() [function.iconv]: Detected an illegal character in input string in C:\\xampp\\htdocs\\web\\bsCMS\\ysmt\\testurl.php on line 12

This is the complete function toAscii:

setlocale(LC_ALL, 'en_US.UTF8');
function toAscii($str, $replace=array(), $delimiter='-') {
    if( !empty($replace) ) {
        $str = str_replace((array)$replace, ' ', $str);
    }

    $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
    $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
    $clean = strtolower(trim($clean, '-'));
    $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);

    return $clean;
}

My guess is I have to sync the character encoding from the form to the toAscii function but how?

Check if this works:

In the HTML, where the form resides, use:

<meta http-equiv="Content-type" content="text/html; charset=utf-8" />

To set as a default character encoding to UTF8. That will make the browser set UTF8 and send the variables in POST in UTF8.

Then in your PHP, use the:

header ('Content-type: text/html; charset=utf-8');

To set the HTTP communication to 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