tags:

views:

398

answers:

3

I want to be able to detect (using regular expressions) if a string contains hebrew characters both utf8 and iso8859-8 in the php programming language. thanks!

A: 

First, such a string would be completely useless - a mix of two different character sets?

Both the hebrew characters in iso8859-8, and each byte of multibyte sequences in UTF-8, have a value ord($char) > 127. So what I would do is find all bytes with a value greater than 127, and then check if they make sense as is8859-8, or if you think they would make more sense as an UTF8-sequence...

gnud
How can a character have ord($char) > 255 in ISO-8859-8? It's a single byte!
Arthur Reutenauer
Well well. I don't know why, but I completely fudged that - non-ascii are between 128 and 255 - fixed now.
gnud
I figured that was what you meant in the mean time. You're lucky I waited before downvoting you ;-)
Arthur Reutenauer
+4  A: 

Here's map of the iso8859-8 character set. The range E0 - FA appears to be reserved for Hebrew. You could check for those characters in a character class:

[\xE0-\xFA]

For UTF-8, the range reserved for Hebrew appears to be 0591 to 05F4. So you could detect that with:

[\u0591-\u05F4]

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

echo preg_match("/[\u0591-\u05F4]/", $string);
Andomar
The problem is that E0-FA are also values that will occur in UTF-8, but not nessescarily as hebrew characters...
gnud
@gnud: That's why you should not use the iso8859-8 regex on UTF-8 strings
Andomar
+1  A: 

Here's a small function to check whether the first character in a string is in hebrew:

function IsStringStartsWithHebrew($string)
{
    return (strlen($string) > 1 && //minimum of chars for hebrew encoding
        ord($string[0]) == 215 && //first byte is 110-10111
        ord($string[1]) >= 144 && ord($string[1]) <= 170 //hebrew range in the second byte.
        );
}

good luck :)