简体   繁体   English

UTF-8字符编码麻烦

[英]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 我正在尝试实现此功能来为我的页面处理干净的url: 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 我收到以下错误:注意:iconv()[function.iconv]:在第12行的C:\\ xampp \\ htdocs \\ web \\ bsCMS \\ ysmt \\ testurl.php中的输入字符串中检测到非法字符

This is the complete function toAscii: 这是Ascii的完整功能:

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? 我的猜测是我必须将字符编码从表单同步到toAscii函数,但是如何?

Check if this works: 检查是否可行:

In the HTML, where the form resides, use: 在表单所在的HTML中,使用:

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

To set as a default character encoding to UTF8. 设置为默认字符编码为UTF8。 That will make the browser set UTF8 and send the variables in POST in UTF8. 这将使浏览器设置UTF8并在UTF8中的POST中发送变量。

Then in your PHP, use the: 然后在您的PHP中,使用:

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

To set the HTTP communication to UTF8. 要将HTTP通信设置为UTF8。

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

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