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 <
Excuse me second-guessing why you're doing this, but..
If this is for search matching: The point of character set collation in a MySQL (for example), is that you can search for "n" and still match "ñ"
IF this is for display purposes: I'd recommend if you have to do this, you do it when you display the text to a user. You can never get your original data back otherwise.
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);
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);
You can try iconv() with ASCII//TRANSLIT
:
$text = iconv("UTF-8", "ASCII//TRANSLIT", $text);