Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP replacing special characters like à->a, è->e

I have php document signup.php which save the content from form (in form.php document) to MySQL base. The problem arises when I want to reformat the input content. I want do decode UTF-8 charachters like à->a.

  $first_name=$_POST['first_name'];   $last_name=$_POST['last_name'];   $course=$_POST['course'];    $chain="prêt-à-porter";  $pattern = array("'é'", "'è'", "'ë'", "'ê'", "'É'", "'È'", "'Ë'", "'Ê'", "'á'", "'à'", "'ä'", "'â'", "'å'", "'Á'", "'À'", "'Ä'", "'Â'", "'Å'", "'ó'", "'ò'", "'ö'", "'ô'", "'Ó'", "'Ò'", "'Ö'", "'Ô'", "'í'", "'ì'", "'ï'", "'î'", "'Í'", "'Ì'", "'Ï'", "'Î'", "'ú'", "'ù'", "'ü'", "'û'", "'Ú'", "'Ù'", "'Ü'", "'Û'", "'ý'", "'ÿ'", "'Ý'", "'ø'", "'Ø'", "'œ'", "'Œ'", "'Æ'", "'ç'", "'Ç'");  $replace = array('e', 'e', 'e', 'e', 'E', 'E', 'E', 'E', 'a', 'a', 'a', 'a', 'a', 'A', 'A', 'A', 'A', 'A', 'o', 'o', 'o', 'o', 'O', 'O', 'O', 'O', 'i', 'i', 'i', 'I', 'I', 'I', 'I', 'I', 'u', 'u', 'u', 'u', 'U', 'U', 'U', 'U', 'y', 'y', 'Y', 'o', 'O', 'a', 'A', 'A', 'c', 'C');   $chain = preg_replace($pattern, $replace, $chain);  echo $chain; // print pret-a-porter  $first_name =  preg_replace($pattern, $replace, $first_name);  echo $first_name; // does not change the input!?! 

Why it works perfectly for $chain, but for $first_name or $last_name doesnt work?

Also i try

echo $first_name; // print áááááábéééééébšššš $trans = array("á" => "a", "é" => "e", "š" => "s"); echo strtr("áááááábéééééébšššš", $trans); // print aaaaaabeeeeeebssss echo strtr($first_name,$trans);  // print áááááábéééééébšššš 

but the problem, as you can see, is same!

like image 238
Zoran Đukić Avatar asked Apr 14 '12 10:04

Zoran Đukić


People also ask

How can I replace special characters in a string in php?

“replace all special characters in php” Code Answer$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens. return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.

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 numbers and special characters from a string in php?

1 Answer. Show activity on this post. function clean($string) { $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens. return preg_replace('/[^A-Za-z\-]/', '', $string); // Removes special chars. }


1 Answers

There

like image 182
siride Avatar answered Sep 21 '22 18:09

siride