regexp with russian lang

前端 未结 3 1741
别那么骄傲
别那么骄傲 2020-12-08 23:41

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         


        
相关标签:
3条回答
  • 2020-12-09 00:28

    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

    0 讨论(0)
  • 2020-12-09 00:33

    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"
    
    0 讨论(0)
  • 2020-12-09 00:36

    This one worked for me:

    /^[а-яА-Я\p{Cyrillic}0-9\s\-]+$/ 
    

    I have tested in all the browsers including Safari

    0 讨论(0)
提交回复
热议问题