3

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.

Special K.
  • 521
  • 1
  • 8
  • 18
  • 3
    "Need this especially for links" <-- wouldn't it be better just to properly encode the URI? :) –  Nov 29 '11 at 18:30
  • See http://stackoverflow.com/questions/1770250/how-to-remove-diacritics-from-text http://stackoverflow.com/questions/3635511/remove-diacritics-from-a-string – Maxime Pacary Nov 29 '11 at 18:32
  • No, because it will result me html characters like s%21 – Special K. Nov 29 '11 at 18:33
  • @Zeus: Most modern browsers (Firefox and Chrome, in particular) will not show the `%` URL encoding syntax when displaying a URL, rather, they will show the result. For example, http://en.wikipedia.org/wiki/R%C3%A9sum%C3%A9 displays as "…/Résumé" in my URL bar, not "`…/R%C3%A9sum%C3%A9`" – Thanatos Nov 30 '11 at 09:13

3 Answers3

9

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.

malletjo
  • 1,766
  • 16
  • 18
3

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 );
}
Khanh Le
  • 111
  • 1
  • 2
0

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 );
}
Ratata Tata
  • 2,781
  • 1
  • 34
  • 49