Search code examples
phpnormalizeturkish

Normalize Turkish in PHP?


Is there a way to simply normalize turkish characters like Ç, Ğ, İ, Ö, Ş, Ü and ı ?

cause now I'm using str_replace but that doesn't seem the right way to go, cause it's possible to forget a character.... Is there a more standard way? I tried to use the normalize method within the PHP internationalization module, but the Turkish characters stay Turkish. I would like to replace them with normal characters for the URL. So Ç becomes C and Ş becomes S, and so on.


Solution

  • What do you mean by normalization? Just take the characters as they come in, but put your scripts, connection and html in correct encoding.

    UTF-8 suggested, explanation: UTF-8 vs. Unicode

    If you only want ASCII chars, you can test this by something like ord($char) < 255.

    For conversion look into these functions:

    http://php.net/iconv

    http://php.net/utf8_encode

    http://php.net/mb_convert_encoding

    A call similiar to

    $str = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
    

    would do the trick.

    Another preg_replace way: Convert special characters to normal characters using PHP, like ã, é, ç to a, e, c