Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if a string contains characters in Hebrew using PHP?

Tags:

regex

php

hebrew

Trying to figure out how to tell whether a string contains any characters in Hebrew with no luck.

How can this be done?

like image 321
Lior Avatar asked Dec 18 '11 00:12

Lior


3 Answers

If the source string is UTF-8 encoded, then the simpler approach would be using \p{Hebrew} in the regex.

The call also should have the /u modifier.

 = preg_match("/\p{Hebrew}/u", $string)
like image 97
mario Avatar answered Sep 19 '22 11:09

mario


map of the iso8859-8 character set. The range E0 - FA appears to be reserved for Hebrew.

[\xE0-\xFA]

For UTF-8, the range reserved for Hebrew appears to be 0590 to 05FF.

[\u0590-\u05FF]

Here's an example of a regex match in PHP:

echo preg_match("/[\u0590-\u05FF]/", $string);
like image 38
JosephRuby Avatar answered Sep 19 '22 11:09

JosephRuby


The simplest approach would be:

preg_match('/[א-ת]/',$string)

For example,

$strings = array( "abbb","1234","aabbאאבב","אבבבב");

foreach($strings as $string)
{
    echo "'$string'  ";

    echo (preg_match('/[א-ת]/',$string))? "has Hebrew characters in it." : "is not Hebrew";

    echo "<br />";
}
like image 24
reshetech Avatar answered Sep 21 '22 11:09

reshetech