Perhaps an error in expression? I'm stuck :(

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
rreimche
Forum Newbie
Posts: 1
Joined: Fri Jun 29, 2012 3:25 pm

Perhaps an error in expression? I'm stuck :(

Post by rreimche »

This script should check if there are any "bad" characters, i.e. if an attack is being commenced:

Code: Select all

$string_exp = "/^[- ,.A-Za-zА-Яа-я0-9]+$/"; 

if(!preg_match($string_exp,$tour_title)) { 
    $error_message .= '<li>Bad data in one the "tour_desc" field.</li>'; 
  } 
Then, if $error_message contains anything, an error message should be displayed instead of sending an email.

I suppose that is the string contains only latin symbols, cyrillic symbols, numbers, comma, dot, space or "-", the check should be OK and the email should be sent. But in reality it seems so that the test gives an error depending on what cyrillic symbols are there in the string.

I tried some examples:

[text]abvgd
абвгд
Абвгда
АбрикосыАпельсины
Абрикосы
Абр[/text]

and some of them work and some don't.

Could anybody point me at the problem please?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Perhaps an error in expression? I'm stuck :(

Post by requinix »

I'm not sure those A-Я ranges aren't working as you expect. But they might be. Regardless,

1. You need UTF-8 support in the expression, otherwise it'll just look like byte ranges which will be completely not what you want.
2. If you put the characters in there literally then you have to make sure the .php file itself is in UTF-8 encoding too. But there's an alternative.

Code: Select all

"/^[- ,.A-Za-z\\x{0410}-\\x{042F}\\x{0430}-\\x{044F}0-9]+$/u"
The /u is for UTF-8 mode and the \x{...} are the Unicode codepoints (eg, Я is U+042F). The file encoding doesn't matter.
Post Reply