Question

In my current PHP code I have many problems with the specials chars and I wan to replace them with a regex. Do you know how I can make that's ?

I want to accept just the :

All letters with accents All numbers ()@-_'&

My regex:

preg_replace('/[^\p{L}0-9\-]/u', '', $string);

That's all. This regex it's to make a clean header to download file.

Thanks you all for your helping. Best regards,

Was it helpful?

Solution

You can just include allowed characters in your character class:

$replaced = preg_replace("/[^\p{L}0-9()@_'&-]+/u", '', $string);

ALso better to use a quantifier + here to make replacement process more efficient since multiple characters will be matched and replaced in each call.

OTHER TIPS

As you're using \pL for letters, you could use \pN for numbers:

preg_replace("/[^\p{L}\p{N}()@_'&-]+/u", '', $string);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top