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

前端 未结 3 973
我在风中等你
我在风中等你 2020-12-18 03:53

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

How can this be done?

相关标签:
3条回答
  • 2020-12-18 04:34

    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 />";
    }
    
    0 讨论(0)
  • 2020-12-18 04:36

    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)
    
    0 讨论(0)
  • 2020-12-18 04:50

    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);
    
    0 讨论(0)
提交回复
热议问题