正则表达式删除特殊字符并保留重音和()@ -_'和字符

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,

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.

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

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