justanotherhobbyist
justanotherhobbyist

Reputation: 2190

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?

Upvotes: 1

Views: 277

Answers (1)

Tomás
Tomás

Reputation: 3561

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.

Upvotes: 2

Related Questions