Skip to content
Advertisement

php preg_replace all but alphanumeric, space and accented letters

I want to strip a string from all characters, except for: Alphanumeric characters, spaces and accented letters.

I got it to work for everything except for the accented letters:

$fname = preg_replace("/[^wspace/", "", $fname);

What do I need to change in order to allow accented letters in the output?

Advertisement

Answer

When I was struggling to get things working, I found the answer myself, so I decided to share it with you:

$fname = preg_replace("/[^wspacepL]/", "", $fname);

The “pL” part matches anything in the Unicode letter category, so accented letters are allowed in the output.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement