Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preg_replace and preg_match arabic characters [duplicate]

Possible Duplicate:
how to add Arabic letters to url regex

I have been searching a couple of hours without an answer.

How do you use preg_replace on arabic characters as well as english?

this is my code is english

$string = preg_replace ( "/&([a-zA-Z])(uml|acute|grave|circ|tilde|ring),/", "", $string );
$string = preg_replace ( "/[^a-zA-Z0-9_.-]/", "", $string );

some of the answers suggested i use this code:

$string = preg_replace ( "/&([أ-يa-zA-Z])(uml|acute|grave|circ|tilde|ring),/u", "", $string );
$string = preg_replace ( "/[^أ-يa-zA-Z0-9_.-]/u", "", $string );

I have tested it and it works. but is this actually functional for php? does it include all arabic characters? is there a better way to include all arabic characters?

What i am going to do with the code is:

Replace all characters in the string to valid SEO friendly characters.

I solved the problem using this code based on http://www.unicodemap.org. Thank you Bryan.

$string = preg_replace ( "/&([\x{0600}-\x{06FF}a-zA-Z])(uml|acute|grave|circ|tilde|ring),/u", "", $string );
    $string = preg_replace ( "/[^\x{0600}-\x{06FF}a-zA-Z0-9_.-]/u", "", $string );
like image 446
Xees Avatar asked Aug 20 '12 23:08

Xees


2 Answers

The unicode character map is a great place to visualize the groups of characters including Arabic that the first part of the string is grouping for you with [أ-يa-zA-Z]

If you are still unsure, read up a little more on regular expressions.

like image 130
Bryan Wolfford Avatar answered Oct 26 '22 01:10

Bryan Wolfford


Something I see in your ranges is, you have [أ-ي] and I know the one on the right is the Arabic A, the first letter. I'm not familiar with the first one, but I suppose it's something like the last character. If that is the case, because PHP language is English, you might want to change the direction of your range into [ي-أ] instead.

Also, for normalization, I would use Unicode character ranges instead, like Bryan suggested.

like image 41
inhan Avatar answered Oct 25 '22 23:10

inhan