Special K.
Special K.

Reputation: 520

How to remove accent from characters? (Leave only English alphabet glyphs)

I need to transform in PHP, a special character like ă -> a, â -> a, ț -> t and so on.

I need this especially for links, so any help would be appreciated.

Upvotes: 3

Views: 2091

Answers (3)

Khanh Le
Khanh Le

Reputation: 111

Update the answer from Orlando, I add some more special char

function clean_special_chars ($s, $d=false) {
if($d) $s = utf8_decode( $s );

$chars = array(
    '_' => '/`|´|\^|~|¨|ª|º|©|®/',
    'a' => '/à|á|ả|ạ|ã|â|ầ|ấ|ẩ|ậ|ẫ|ă|ằ|ắ|ẳ|ặ|ẵ|ä|å|æ/',
    'd' => '/đ/',
    'e' => '/è|é|ẻ|ẹ|ẽ|ê|ề|ế|ể|ệ|ễ|ë/',
    'i' => '/ì|í|ỉ|ị|ĩ|î|ï/',
    'o' => '/ò|ó|ỏ|ọ|õ|ô|ồ|ố|ổ|ộ|ỗ|ö|ø/',
    'u' => '/ù|ú|û|ũ|ü|ů|ủ|ụ|ư|ứ|ừ|ữ|ử|ự/',
    'A' => '/À|Á|Ả|Ạ|Ã|Â|Ầ|Ấ|Ẩ|Ậ|Ẫ|Ă|Ằ|Ắ|Ẳ|Ặ|Ẵ|Ä|Å|Æ/',
    'D' => '/Đ/',
    'E' => '/È|É|Ẻ|Ẹ|Ẽ|Ê|Ề|Ế|Ể|Ệ|Ễ|Ê|Ë/',
    'I' => '/Ì|Í|Ỉ|Ị|Ĩ|Î|Ï/',
    'O' => '/Ò|Ó|Ỏ|Ọ|Õ|Ô|Ồ|Ố|Ổ|Ộ|Ỗ|Ö|Ø/',
    'U' => '/Ù|Ú|Û|Ũ|Ü|Ů|Ủ|Ụ|Ư|Ứ|Ừ|Ữ|Ử|Ự/',
    'c' => '/ć|ĉ|ç/',
    'C' => '/Ć|Ĉ|Ç/',
    'n' => '/ñ/',
    'N' => '/Ñ/',
    'y' => '/ý|ỳ|ỷ|ỵ|ỹ|ŷ|ÿ/',
    'Y' => '/Ý|Ỳ|Ỷ|Ỵ|Ỹ|Ŷ|Ÿ/'
);

return preg_replace( $chars, array_keys( $chars ), $s );
}

Upvotes: 3

Ratata Tata
Ratata Tata

Reputation: 2879

You can use this:

function clean_special_chars( $s, $d=false )
{
    if($d) $s = utf8_decode( $s );

    $chars = array(
    '_' => '/`|´|\^|~|¨|ª|º|©|®/',
    'a' => '/à|á|â|ã|ä|å|æ/', 
    'e' => '/è|é|ê|ë/', 
    'i' => '/ì|í|î|ĩ|ï/',   
    'o' => '/ò|ó|ô|õ|ö|ø/', 
    'u' => '/ù|ú|û|ű|ü|ů/', 
    'A' => '/À|Á|Â|Ã|Ä|Å|Æ/', 
    'E' => '/È|É|Ê|Ë/', 
    'I' => '/Ì|Í|Î|Ĩ|Ï/',   
    'O' => '/Ò|Ó|Ô|Õ|Ö|Ø/', 
    'U' => '/Ù|Ú|Û|Ũ|Ü|Ů/', 
    'c' => '/ć|ĉ|ç/', 
    'C' => '/Ć|Ĉ|Ç/', 
    'n' => '/ñ/', 
    'N' => '/Ñ/', 
    'y' => '/ý|ŷ|ÿ/', 
    'Y' => '/Ý|Ŷ|Ÿ/'
    );

return preg_replace( $chars, array_keys( $chars ), $s );
}

Upvotes: 0

malletjo
malletjo

Reputation: 1786

When i want to get plain text (from utf-8) i'm using iconv.

iconv('utf8', 'ascii//TRANSLIT', $text);

If it's only for your url, urlencode may be a better idea.

Upvotes: 9

Related Questions