Possible Duplicate:
how to replace special characters with the ones they’re based on in PHP?
I have a string that looks like this:
ABCÇĆDEFGHÎIïJ123450086
In PHP how can I make it appear as:
ABCDEFGHIJ123450086
without having to manually replace each character not needed. Can I use some kind of RegEx for this? How?
I just want A-Z and 0-9, no other foreign characters (as in, remove them).
Use character classes:
$string = preg_replace('/[^\w\d]/', '', $string);
Replaces all occurences of characters which are not ([^]
) alphabetic (\w
), nor a digit (\d
) with an empty string.
Assuming you want to remove them, You can use preg_replace to replace all characters that are not in the ranges a-z, A-Z and 0-9 with '';
Otherwise use the translation technique given in the other thread.
You can always use regular expressions.
preg_replace('/^[A-Za-z0-9]/', '', $some_str)
Use a whitelist:
$input = 'ABCÇĆDEFGHÎIïJ123450086';
$filtered = preg_replace("~[^a-zA-Z0-9]+~","", $input);
A nice function :
/**
* Strip accents
*
* @param string $str string to clean
* @param string $encoding encoding type (example : utf-8, ISO-8859-1 ...)
*/
function strip_accents($str, $encoding='utf-8') {
// transforme accents chars in entities
$str = htmlentities($str, ENT_NOQUOTES, $encoding);
// replace entities to have the first nice char
// Example : "&ecute;" => "e", "&Ecute;" => "E", "Ã " => "a" ...
$str = preg_replace('#&([A-za-z])(?:acute|grave|cedil|circ|orn|ring|slash|th|tilde|uml);#', '\1', $str);
// Replace ligatures like : Œ, Æ ...
// Example "Å“" => "oe"
$str = preg_replace('#&([A-za-z]{2})(?:lig);#', '\1', $str);
// Delete else
$str = preg_replace('#&[^;]+;#', '', $str);
return $str;
}
// Example
$texte = 'Ça va mon cœur adoré?';
echo suppr_accents($texte);
// Output : "Ca va mon coeur adore?"
Source : http://www.infowebmaster.fr/tutoriel/php-enlever-accents