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 );
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With