Search code examples
phpstringreplacespecial-characterscharacter-encoding

how to replace special characters with the ones they're based on in PHP?


How do I replace:

  • "ã" with "a"
  • "é" with "e"

in PHP? Is this possible? I've read somewhere I could do some math with the ascii value of the base character and the ascii value of the accent, but I can't find any references now.


Solution

  • 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.