CNk CNk - 1 month ago 12
PHP Question

Preg Match for Letters in Arabic and Numbers in English

I want to allow my users to enter such record - Letters in Arabic and Numbers in english. (عسا871)

And I run a preg match on the top, I tried it this way -

if(!empty($_POST['number']))
if(!preg_match('/^[0-9]|[\p{Arabic}]+$/',$_POST["number"]))
die("Number Data Modification");


It still does not accept Arabic, what is a right way to do it?

Answer

The ^[0-9]|[\p{Arabic}]+$ regex accepts strings that have 1 ASCII digit at the start of string (^[0-9]) or (|) Arabic letters at the end ([\p{Arabic}]+).

Most probably you want to allow any string consisting of either ASCII digits or Arabic letters:

'/^[0-9\p{Arabic}]+$/u'

See the regex demo