سؤال

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);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top