Problem replacing accented characters

Asked

Viewed 75 times

0

The function preg_replace is not behaving the way I predicted. By executing the code $string = preg_replace("/[ÁÀÂÃÄáàâãä]/", "a", $string); what I get back is a string filled with "a"s. If the string has "Áàâäàâäíììì" the answer from preg_replace is "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaiaiai". Can you explain to me why this happens? And what is the best way to make this replacement?

  • Use the flag u in regex (logo deopis of the second bar): preg_replace("/[ÁÀÂÃÄáàâãä]/u", "a", $string)

1 answer

0

You can use the incov to do so:

preg_replace('/[^a-z]/i', '', iconv("UTF-8", "US-ASCII//TRANSLIT", $string))

If I understand your problem correctly, this must be the resolution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.