I want to prevent my visitors who want to become users in my websites from using Latin characters.
I want them to use only Arabic user names.
How to do that with regex and preg_match?
I want to prevent my visitors who want to become users in my websites from using Latin characters.
I want them to use only Arabic user names.
How to do that with regex and preg_match?
I think this has been answered here -> stackoverflow.com - How do I detect if an input string is Arabic
<?
function uniord($u) {
// i just copied this function fron the php.net comments, but it should work fine!
$k = mb_convert_encoding($u, 'UCS-2LE', 'UTF-8');
$k1 = ord(substr($k, 0, 1));
$k2 = ord(substr($k, 1, 1));
return $k2 * 256 + $k1;
}
function is_arabic($str) {
if(mb_detect_encoding($str) !== 'UTF-8') {
$str = mb_convert_encoding($str,mb_detect_encoding($str),'UTF-8');
}
/*
$str = str_split($str); <- this function is not mb safe, it splits by bytes, not characters. we cannot use it
$str = preg_split('//u',$str); <- this function woulrd probably work fine but there was a bug reported in some php version so it pslits by bytes and not chars as well
*/
preg_match_all('/.|\n/u', $str, $matches);
$chars = $matches[0];
$arabic_count = 0;
$latin_count = 0;
$total_count = 0;
foreach($chars as $char) {
//$pos = ord($char); we cant use that, its not binary safe
$pos = uniord($char);
echo $char ." --> ".$pos.PHP_EOL;
if($pos >= 1536 && $pos <= 1791) {
$arabic_count++;
} else if($pos > 123 && $pos < 123) {
$latin_count++;
}
$total_count++;
}
if(($arabic_count/$total_count) > 0.6) {
// 60% arabic chars, its probably arabic
return true;
}
return false;
}
$arabic = is_arabic('عربية إخبارية تعمل على مدار اليوم. يمكنك مشاهدة بث القناة من خلال الموقع');
var_dump($arabic);
?>
They also show a preg_match statement $str = "بسم الله"; if (preg_match('/[أ-ي]/ui', $str)) { echo "A match was found."; } else { echo "A match was not found."; }
You could use the unicode property \p{Arabic}, see http://perldoc.perl.org/perluniprops.html
Using php:
preg_match('/^\p{Arabic}+$/u', $string, $matches);