简体   繁体   中英

Can't convert UTF-8 'ø' to ASCII

Function converts all other characters, just 'ø' what is UTF-8 character not, all other chars, like "Ч,Č,Ć,Đ,Š,Ž,Ђ,Ж,Љ" etc. converts normally to ascii...

This is function what i use:

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;
}

I need it to ascii for url.

The iconv "transliterate to ascii" function is unreliable. There isn't always a universal transliteration or a transliteration that makes sense from any arbitrary unicode codepoint to ascii. There isn't a unicode standard saying how to do it (although at one point there was a draft one, it was abandoned as unsuccessful). So, anyway, there isn't a reliable way to do this, or the iconv function isn't one. Just how it goes.

As others have commented, there is a standard way to put unicode into a URL though. Trying to transliterate arbitrary unicode codepoints to ascii is unlikely to be the right solution to your problem; even for the transliteration that is happening, you are likely losing meaning.

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