Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regexp with russian lang

I can't solve my problem with regexp.

Ok, when i type:

$string = preg_replace("#\[name=([a-zA-Z0-9 .-]+)*]#","$name_start $1 $name_end",$string);

everything is ok, except situation with Russian language.

so, i try to re-type this reg-exp:

$string = preg_replace("#\[name=([a-zA-Z0-9**а-яА-Я** .-]+)*]#","$name_start $1 $name_end",$string);

but this not working,

i know some idea, just write:

$string = preg_replace("#\[name=([a-zA-Z0-9йцукенгшщзхъфывапролджэячсмитьбю .-]+)*]#","$name_start $1 $name_end",$string);

but this is crazy :D

please, give me simple variant

like image 251
vorobey Avatar asked Oct 15 '09 09:10

vorobey


2 Answers

The common unicode script (supported since pcre 3.3) provides a test for the property Cyrillic.

e.g. replace all characters that are neither cyrillic nor (latin) digits:

$string = '1a2b3cйdцeуfкбxюy';
echo preg_replace('/[^0-9\p{Cyrillic}]/u', '*', $string);

You can find the documentation for that feature under http://www.pcre.org/pcre.txt "Unicode character properties".
And you have to specify the PCRE8 flag (u) as described at http://docs.php.net/reference.pcre.pattern.modifiers

like image 187
VolkerK Avatar answered Nov 03 '22 02:11

VolkerK


Try a Unicode range:

'/[\x{0410}-\x{042F}]/u'  // matches a capital cyrillic letter in the range A to Ya

Don't forget the /u flag for Unicode.

In your case:

"#\[name=([a-zA-Z0-9\x{0430}-\x{044F}\x{0410}-\x{042F} .-]+)*]#u"

Note that the STAR in your regex is redundant. Everything already gets "eaten" by the PLUS. This would do the same:

"#\[name=([a-zA-Z0-9\x{0430}-\x{044F}\x{0410}-\x{042F} .-]+)]#u"
like image 29
Bart Kiers Avatar answered Nov 03 '22 01:11

Bart Kiers