try this .. works for me.
iconv('utf-8', 'ascii//TRANSLIT', $text);
Answer from Stewie on Stack OverflowThere's a great tip from this page: How to remove diacritics from text? Here's my version of it:
/** Normalize a string so that it can be compared with others without being too fussy.
* e.g. "Ádrèñålînë" would return "adrenaline"
* Note: Some letters are converted into more than one letter,
* e.g. "ß" becomes "sz", or "æ" becomes "ae"
*/
function normalize_string($string) {
// remove whitespace, leaving only a single space between words.
$string = preg_replace('/\s+/', ' ', $string);
// flick diacritics off of their letters
$string = preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml|caron);~i', '$1', htmlentities($string, ENT_COMPAT, 'UTF-8'));
// lower case
$string = strtolower($string);
return $string;
}
It's good because, unlike the iconv method mentioned above, there's no converting between character sets (they're a minefield).
I copied and pasted your code into my editor and something interesting happened. Instead of getting adios I was getting adjiós. Notice the j in the middle after the d. This was coming from the 'đ'=>'dj', in the first line of the table map. Apparently, my editor changed the đ to a regular d, and then it wouldn't convert the ó. I removed this key/value pair and suddenly it worked for me. Are you sure all of your keys are correct in your editor (Does you editor accept alternative character sets?) Here is my test file (with the đ removed:
<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
</head>
<body>
<?php
function normalize ($string) {
$table = array(
'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj', 'Ž'=>'Z', 'ž'=>'z', 'C'=>'C', 'c'=>'c', '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', 'r'=>'r',
);
return strtr($string, $table);
}
$word = 'adiós';
$length = strlen($word);
echo 'original: '. $word;
echo '<br />';
echo 'normalized: '. normalize($word);
echo '<br />';
echo 'loop: ';
for($i = 0; $i < $length; $i++) {
echo normalize($word[$i]);
}
?>
</body>
</html>
When I loop through each character with the 'd' => 'dj' in the array map then I correctly get adjios
If you don't have access to the Normalizer class or just don't wish to use it you can use the following function to replace most (all?) of the common accentuations.
function Unaccent($string)
{
return preg_replace('~&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($string, ENT_QUOTES, 'UTF-8'));
}
This answer is incorrect. I didn't understand Unicode Normalization when I wrote it. Look at francadaval's comment and link
Check out the Normalizer class to do this. The documentation is good, so I'll just link it instead of repeating things here:
http://www.php.net/manual/en/class.normalizer.php
Specifically, the normalize member of that class:
http://www.php.net/manual/en/normalizer.normalize.php
Note that Unicode normalization has several forms, and you seem to want Normalization Form KD (NFKD) Compatibility Decomposition, though you should read the documentation to make sure.
You shouldn't try to roll your own function for this: There's way too many things that can go wrong, and using the provided function is a much better idea.
$newstr = preg_replace('/[^a-zA-Z0-9\']/', '_', "There wouldn't be any");
$newstr = str_replace("'", '', $newstr);
I put them on two separate lines to make the code a little more clear.
Note: If you're looking for Unicode support, see Filip's answer below. It will match all characters that register as letters in addition to A-z.
If you by writing "non letters and numbers" exclude more than [A-Za-z0-9] (ie. considering letters like åäö to be letters to) and want to be able to accurately handle UTF-8 strings \p{L} and \p{N} will be of aid.
\p{N}will match any "Number"\p{L}will match any "Letter Character", which includes- Lower case letter
- Modifier letter
- Other letter
- Title case letter
- Upper case letter
Documentation PHP: Unicode Character Properties
$data = "Thäre!wouldn't%bé#äny";
$new_data = str_replace ("'", "", $data);
$new_data = preg_replace ('/[^\p{L}\p{N}]/u', '_', $new_data);
var_dump (
$new_data
);
output
string(23) "Thäre_wouldnt_bé_äny"