Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 944
Waleed Asender Avatar asked Oct 23 '12 09:10

Waleed Asender


People also ask

How do I remove non alphabetic characters from a string?

The approach is to use the String. replaceAll method to replace all the non-alphanumeric characters with an empty string.

How do I remove non character characters from a string in Python?

How do you remove non letters from a string in Python? Use the isalnum() Method to Remove All Non-Alphanumeric Characters in Python String. Use the filter() Function to Remove All Non-Alphanumeric Characters in Python String. Use Regular Expressions to Remove All Non-Alphanumeric Characters in Python String.

How do you remove all alphanumeric characters from a string in Python?

Here in our tutorial, let us do this using isalnum(). We can also do this using regular expressions and isalpha(), isnumeric(). Most importantly, we use isalnum() because it is the easiest way to delete alphanumeric elements from the list.


1 Answers

Try the below:

$slug = preg_replace('/[^\p{Arabic}\da-z-]/ui', '', $string);
like image 82
xdazz Avatar answered Sep 19 '22 09:09

xdazz