Saturday, March 20, 2010

Tutorial: Validating User's Arabic Input using Regular Expressions in PHP

In PHP, Regular Expressions can be used to match a string to a specific pattern, this can be used to validate user's input to avoid certain security problems such as spoofed form submissions and cross-site scripting (see http://phpsec.org/projects/guide/), this can be done using preg_match() function

Here I'll talk only about using preg_match() to validate arabic input, for an explanation of regular expressions in PHP, see http://www.phpro.org/tutorials/Introduction-to-PHP-Regex.html

First Method you can use to make regular expressions include arabic is using \p{Arabic}, this makes preg_match() accept arabic strings,

that's not completely true :D

The previous paragraph is maybe what you would get if you searched the Internet for arabic patterns, but it needs following modifications to work correctly:

  • use /u modifier so the pattern will be treated as UTF-8 (you can make it for all your patterns since it works only for alphanumeric characters) >> '/\p{Arabic}/u'

  • use the HTML META tag so that Request parameters (GET & POST) will be encoded as unicode (the charset attribute should be UTF-8)

those two steps are important to make your validation work

another method is using arabic characters unicode codes, you can use /\x{****}/u to represent any unicode character, also /[\x{****}-\x{----}]/u makes the pattern support any character/symbol which its unicode lies between **** and ----, but do not forget the /u modifier and setting charset to UTF-8

6 comments:

Amr Ebaid said...

Glad you solved it...
:)

ahmed el-Morshedy said...

thx Amr :)
I was sure you'll see that post ;)

Bashar said...

Thanks a lot :)

mervat mosaad said...

pleeeeeeeeeeeeas,i can't understand Regular Expression Syntax in java ,can any person help me :(

Amr Ebaid said...

صاحب البوست محبوس دلوقتي في سجون مصر بلد الظلم والظلمات :(

Anonymous said...

اللهم لا تدع لنا ذنبا إلا غفرته ، ولا هما إلا فرجته ، ولا دينا إلا قضيته ، ولا مريضا إلا شفيته ، ولا مبتلى إلا عافيته ، ولا ضالا إلا هديته

ولا غائبا إلا رددته ، ولا مظلوما إلا نصرته ، ولا أسيرا إلا فككته ،
ولا غائبا إلا رددته ، ولا مظلوما إلا نصرته ، ولا أسيرا إلا فككته
ولا غائبا إلا رددته ، ولا مظلوما إلا نصرته ، ولا أسيرا إلا فككته

ولا ميتا إلا رحمته ، ولا حاجة لنا فيها صلاح ولك فيها رضا إلا قضيتها ويسرتها بفضلك يا أكرم الأكرمين.

!!!!
#الحرية_للمرشدى