Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert accented characters to their plain ascii equivalents

I have to convert french characters into english on my php. I've used the following code:

iconv("utf-8", "ascii//TRANSLIT", $string); 

But the result for ËËË was "E"E"E.

I don't need that double quote and other extra characters - I want to show an output like EEE. Is there any other method to convert french to english? Can you help me to do this?

like image 914
ram Avatar asked Apr 07 '12 13:04

ram


People also ask

How do I change the accented characters to regular characters in Excel?

Click Kutools > Text > Replace Accented Characters…, see screenshot: 3. In Replace Accented Characters dialog box, click the Select all button to select all replace rules, and click the Ok button to replace all accented characters.

How can I replace an accented character with a normal character in PHP?

php $transliterator = Transliterator::createFromRules(':: NFD; :: [:Nonspacing Mark:] Remove; :: NFC;', Transliterator::FORWARD); $test = ['abcd', 'èe', '€', 'àòùìéëü', 'àòùìéëü', 'tiësto']; foreach($test as $e) { $normalized = $transliterator->transliterate($e); echo $e.

How do I remove the accented character in Java?

Use java. text. Normalizer to handle this for you. This will separate all of the accent marks from the characters.

What is the best way to remove accents normalize in a Python unicode string?

Unidecode is the correct answer for this. It transliterates any unicode string into the closest possible representation in ascii text.


1 Answers

The PHP Manual iconv Intro has a warning:

Note that the iconv function on some systems may not work as you expect. In such case, it'd be a good idea to install the GNU libiconv library. It will most likely end up with more consistent results.

But if accented characters are the only issue then you could use a dirty strtr (partially from strtr comments):

$string = 'Ë À Ì Â Í Ã Î Ä Ï Ç Ò È Ó É Ô Ê Õ Ö ê Ù ë Ú î Û ï Ü ô Ý õ â û ã ÿ ç';  $normalizeChars = array(     'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj','Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A',     'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I',     'Ï'=>'I', 'Ñ'=>'N', 'Ń'=>'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', 'ń'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u',     'ú'=>'u', 'û'=>'u', 'ü'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'ƒ'=>'f',     'ă'=>'a', 'î'=>'i', 'â'=>'a', 'ș'=>'s', 'ț'=>'t', 'Ă'=>'A', 'Î'=>'I', 'Â'=>'A', 'Ș'=>'S', 'Ț'=>'T', );  //Output: E A I A I A I A I C O E O E O E O O e U e U i U i U o Y o a u a y c echo strtr($string, $normalizeChars); 
like image 133
Ingmar Boddington Avatar answered Oct 19 '22 17:10

Ingmar Boddington