Last active
April 7, 2021 15:28
-
-
Save NBoulfroy/eefb5868edebaede5143e5bb5709d9a6 to your computer and use it in GitHub Desktop.
[Remove accents from string] Function to replace accents #PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @param string $string | |
* | |
* @return string | |
*/ | |
public function toNoAccent(string $string) | |
{ | |
$accent = array( | |
'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', | |
'à', 'á', 'â', 'ã', 'ä', 'å', | |
'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', | |
'ò', 'ó', 'ô', 'õ', 'ö', 'ø', | |
'È', 'É', 'Ê', 'Ë', | |
'è', 'é', 'ê', 'ë', | |
'Ç', 'ç', | |
'Ì', 'Í', 'Î', 'Ï', 'ì', 'í', 'î', 'ï', | |
'Ù', 'Ú', 'Û', 'Ü', 'ù', 'ú', 'û', 'ü', | |
'ÿ', 'Ñ', 'ñ', | |
); | |
$noAccent = array( | |
'A', 'A', 'A', 'A', 'A', 'A', | |
'a', 'a', 'a', 'a', 'a', 'a', | |
'O', 'O', 'O', 'O', 'O', 'O', | |
'o', 'o', 'o', 'o', 'o', 'o', | |
'E', 'E', 'E', 'E', | |
'e', 'e', 'e', 'e', | |
'C', 'c', | |
'I', 'I', 'I', 'I', 'i', 'i', 'i', 'i', | |
'U', 'U', 'U', 'U', 'u', 'u', 'u', 'u', | |
'y', 'N', 'n', | |
); | |
// Replaces non letter or digits by - | |
$return = str_replace($accent, $noAccent, $string); | |
$return = str_replace('"', '"', $str); | |
return $string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment