Remove garbage characters in arabic

Viewed 4815

I needed to remove all non Arabic characters from a string and eventually with the help of people from stack-overflow was able to come up with the following regex to get rid of all characters which are not Arabic.

preg_replace('/[^\x{0600}-\x{06FF}]/u','',$string);

The problem is the above removes white spaces too. And now I discovered I would need character from A-Z,a-z,0-9, !@#$%^&*() also. So how do I need to modify the regex?

Thanking you

3 Answers

assume you have this string:

$str = "Arabic Text نص عربي test 123 و,.m,............ ~~~ ٍ،]ٍْ}~ِ]ٍ}";

this will keep arabic chars with spaces only.

echo preg_replace('/[^أ-ي ]/ui', '', $str);

this will keep Arabic and English chars with Numbers Only

echo preg_replace('/[^أ-يA-Za-z0-9 ]/ui', '', $str);

this will answer your question latterly.

echo preg_replace('/[^أ-يA-Za-z !@#$%^&*()]/ui', '', $str);
Related