Search code examples
phpregexutf-8arabicereg-replace

Strip non alphanumeric characters from Arabic UTF8 + English string


I want to remove all non Arabic, non English and non Numbers charecters from a string, except for dashes (-).

I managed to do it for non English alphanumeric characters like this:

$slug = ereg_replace('[^A-Za-z0-9-]', '', $string);

But for non arabic alphanumeric characters i tried to do it like this:

$slug = ereg_replace('\p{InArabic}', '', $string);

but it didnt strip the non alphanumeric characters! I also tried this answer but it didnt work either, it always returns '0' !!

$slug = preg_replace('/[^\x{0600}-\x{06FF}A-Za-z0-9-]/u','', $string);

Hopefully someone can help me.


Solution

  • Try the below:

    $slug = preg_replace('/[^\p{Arabic}\da-z-]/ui', '', $string);