tags:

views:

43

answers:

3

Hi,

I have a site where people can submit links to sites about iPhone apps. The guy submits the application name, description, category and URL. This site has years and never received any constructive submission from a russian developer but, unfortunately it was discovered by russian spammers that annoys the hell out of me. Even with all measures against spam, as caption boxes, etc., some guys insist on sending porn russian stuff that has nothing to do with iPhone.

I would like to ban completely any URL or post that is done using russian characters. For URLs I have not much to do, except checking if the URL contains ".ru". But for descriptions, I would like to detect russian characters. How do I do that in PHP?

thanks.

+1  A: 

I would download the Russian alphabet and then check the input string with strstr(). For example:

$russianChars = array('з', 'я'.. etc);

foreach($russianChars as $char) {
    if(strstr($input, $char)) {
        // russian char found in input, do something
    }
}

A good algorithm would probably do something after finding 3 Russian chars or so, to be sure that the language is actually Russian (since Russian chars may show up in other languages, I suggest doing some research if that's the case).

Luca Matteis
+2  A: 

Да очень просто It is easy to do with UTF-8 regular expressions (assuming your site uses UTF-8 encoding):

function isRussian($text) {
    return preg_match('/[А-Яа-яЁё]/u', $text);
}
Alexander Konstantinov
I tested all 3 methods. Yours is the one that works in my case. And I see by your name, that you are probably from there! :-) Thanks.
Digital Robot
@Mike, almost, I'm Russian-speaking but not from Russia :)
Alexander Konstantinov
Cool... I love those russian characters... it is a language that seems to be written in a mirror... :-)
Digital Robot
A: 

now.. this code is about 5 years old, and 'worked for me' back when I had a similar problem

function detect_cyr_utf8($content)
{
  return preg_match('/&#10[78]\d/', mb_encode_numericentity($content, array(0x0, 0x2FFFF, 0, 0xFFFF), 'UTF-8'));
}

thus no warranty, no any of the kind - but it may help you out (basically it encodes all foreign entities then checks for common cyrillic chars)

Best!

nathan
thanksssssssssssssssssss!
Digital Robot