I need to be able to replace some common foreign characters with English equivalents before I store values into my db.
For example: æ replace with ae and ñ with n.
Do I use preg_replace?
Thanks
To replace or substitute all occurrences of one character with another character, you can use the substitute function. The SUBSTITUTE function is full automatic. All you need to do is supply "old text" and "new text". SUBSTITUTE will replace every instance of the old text with the new text.
To replace special characters, use replace() in JavaScript.
You can define your convertable characters in an array, and use str_replace()
:
$conversions = array(
"æ" => "ae",
"ñ" => "n",
);
$text = str_replace(array_keys($conversions), $conversions, $text);
For single character of accents
$str = strtr($str,
"ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÝßàáâãäåçèéêëìíîïñòóôõöøùúûüýÿ",
"AAAAAACEEEEIIIINOOOOOOYSaaaaaaceeeeiiiinoooooouuuuyy");
For double character
of accents (such as Æ, æ
)
$match = array('æ', 'Æ');
$replace = array('ae', 'AE');
$str = str_replace($replace, $replace, $str);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With