PHP replace special characters from a string

Viewed 18281

I have clean function for remove special caracter from string but that function also removing Turkish caracter (ı,ğ,ş,ç,ö) from string

function clean($string) {
   $string = str_replace(' ', ' ', $string); 
   $string = preg_replace('/[^A-Za-z0-9\-]/', ' ', $string); 

   return preg_replace('/-+/', '-', $string); 
}

How can I fix it ?

4 Answers

You can use iconv to replacing special characters like à->a, è->e

<?php
    $string = "ʿABBĀSĀBĀD";
    echo iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $string);
    // output: [nothing, and you get a notice]
    echo iconv('UTF-8', 'ISO-8859-1//IGNORE', $string);
    // output: ABBSBD
    echo iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $string);
    // output: ABBASABAD
    // Yay! That's what I wanted!
    ?>

Credits:

https://gist.github.com/swas/10643194

@dmp y @Nisse Engström

Related