0

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?

Opal
  • 81,889
  • 28
  • 189
  • 210
Said Aha
  • 45
  • 5

3 Answers3

0

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);
?>
Community
  • 1
  • 1
Damo85
  • 65
  • 8
0

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."; }

Damo85
  • 65
  • 8
  • I'm not a coder or programmer i don't understand php syntax so i can't make any change on that code to solve my issue. i just want a simple line to prevent non arabic characters with a "return t" in the end as error message to the user who is not gonna respect that rule – Said Aha May 17 '15 at 13:11
  • I want something like this " if (preg_match('/(\w)\1{2,}/u', $name)) { return t('3 Repeated charachter Are not allowed'); } " – Said Aha May 17 '15 at 13:16
0

You could use the unicode property \p{Arabic}, see http://perldoc.perl.org/perluniprops.html

Using php:

preg_match('/^\p{Arabic}+$/u', $string, $matches);
Toto
  • 89,455
  • 62
  • 89
  • 125
  • @SaidAha: How doesn't it work? Could you please add in your question a sample string and expected result? – Toto May 22 '15 at 12:04
  • i mean it did the opposite of what i suppose to do.. matched the arabic characters and returned an error message that i added it .. i want the code to match every other language characters and return error message except of Arabic characters witch it will be allowed only – Said Aha May 22 '15 at 12:34
  • @SaidAha: Then just reverse the condition should work bit I'm not sure to well understand your needs. Edit your question and add it some test cases. – Toto May 22 '15 at 12:45
  • My question is super clear !!!!! i don't want users to register user names with different characters than arabic. i want to prevent them from doing that because the site is Arabic only. how hard is that to understand ? – Said Aha May 22 '15 at 12:49
  • @SaidAha: OK, then what doesn't work with my regex? It match word that contains only arabic letters. – Toto May 22 '15 at 13:22
  • I changed your code to {Latin} and it works .. but the problem now is that it still can accept a combination of Latin characters and numbers. i want to prevent that how ? – Said Aha May 23 '15 at 11:58