Strip non alphanumeric characters from Arabic UTF8 + English string

arabic, ereg-replace, php, regex, utf-8

Solution

Try the below:

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

Problem

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.

Original source

Related problems