doupin1073 2014-05-20 15:22
浏览 50
已采纳

PHP正则表达式对西里尔字符集不区分大小写

I am using preg_replace and preg_match with PHP, working in this charset: Cyrillic Windows 1251. I am trying to match a word using the case-insensitive modifier.

I made these tests :

$pattern = '/myCyrillicWord1|myCyrillicWord2/i';
$subject = 'Am I able to find MYCyrILlicWord1?';
$res = preg_replace($pattern, 'matched', $subject);

On UTF-8 :

With the utf-8 modifier in the pattern :

$pattern = '/myCyrillicWord1|myCyrillicWord2/iu';
$output = 'Am I able to find matched or not';

Without :

$pattern = '/myCyrillicWord1|myCyrillicWord2/i';
$output = 'Am I able to find MYCyrILlicWord1 or not';

On Windows 1251 :

$pattern = '/myCyrillicWord1|myCyrillicWord2/i';
$output = 'Am I able to find MYCyrILlicWord1 or not';

The regex is functionnal on utf-8 but not on Windows 1251. Please notice that I had tested with cyrillics characters like 'х' and 'Х' (which look like latin letters 'x' and 'X').

My question is to know if that behavior is normal ?

How can I match my cyrillics words in Windows 1251 charset with the case-insensitive modifier ?

Many thanks.

  • 写回答

1条回答 默认 最新

  • donglong7338 2014-05-20 17:43
    关注

    I don't think PCRE supports charsets, so your options are basically

    • convert everything to utf8, process and then convert back, or
    • use manually crafted regexes for case-insensitivity, like /[Дд][Ыы][Кк]/ to match Дык, дыК etc
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退