3

User may input character like Ā Ē ú, how to remove that mark and become A E u in php

Thanks

Chris Eberle
  • 47,994
  • 12
  • 82
  • 119
Hadi
  • 613
  • 1
  • 6
  • 7
  • Actually, you should look at supporting just non-ASCII languages because there are a lot more characters that don't look like an english letter. No reason to remove accents for anything other than URL's and filenames. At any rate, just search this site for "[remove accents](http://stackoverflow.com/a/3542850/99923)". – Xeoncross Jan 28 '12 at 04:30
  • Specifically, the question [Remove accents without using iconv](http://stackoverflow.com/questions/3542818/remove-accents-without-using-iconv/3542850) is a near-duplicate of this question. – Jim DeLaHunt Jan 28 '12 at 04:46

3 Answers3

3

You could write a simple translation data map:

$map = array(
 'Ā' => 'A',
 'Ē' => 'E',
 'ú' => 'u',
 // etc...
);

$title = strtr($title,$map);

http://us2.php.net/strtr

AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
  • 1
    That's a rather unmaintainable solution, unless you know your users will only input a certain subset of characters. – deceze Jan 28 '12 at 04:43
  • It's up to the OP to take this idea and apply it to a permanent solution for his project. Point being, I think `strtr` is the correct way to go here. – AlienWebguy Jan 28 '12 at 16:40
3

You can use:

iconv('UTF-8', 'ASCII//TRANSLIT', 'Ā Ē ú');

You could make a function holding a array of chars you want exchanged and pass strings through and just change ã to a that way, if iconv() doesn´t work out for you like:

$chars = array(
        'Š'=>'S', 'š'=>'s', 'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c',
        'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
        'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
        'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss',
        'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
        'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
        'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b',
        'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r',
    );

$value = strtr($value, $chars);
Sabari
  • 6,205
  • 1
  • 27
  • 36
0

1 - You can use the iconv library (http://www.php.net/manual/en/book.iconv.php), but it's not reliable if you can't determine if you have the libiconv library installed.

2 - You can manually replace the special characters, like this:

$chars = array(
        'Š'=>'S', 'š'=>'s', 'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c',
        'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
        'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
        'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss',
        'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
        'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
        'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b',
        'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r',
    );

    $value = strtr($value, $chars);

3 - You can use the Transliterator and/or the Normalizer classes, but the require PHP 5.4 and PHP 5.3 respectively.

guhemama
  • 107
  • 3
  • 7