dsdv76767671 2014-07-16 04:14
浏览 69
已采纳

preg_match_all有特殊字符

I am trying to do a preg_match_all on data that contains multiple phrases that I want to extract.

Data:

'us/Llane/Hówl' then some other text then 'us/Casey/Hówl' and so on

I would like to extract the 2 names Llane and Casey into an array. I am currently using http://www.phpliveregex.com/ as well as my code itself to try and work this out but regex seems very difficult to understand even with some of the great guides on the internet. To the best of my knowledge this should work:

preg_match_all("/us\/(.*?)\/HÓWL'/",$data,$output);

But all I am getting is $output[0] and $output[1] which are both blank. I wasn't having a problem before so it might be the special character however I can only find information on preg_match_all for detecting special characters, not just using them in a string. Any assistance would be great, I have been stuck on this problem for about 4 days straight now and spent a significant number of hours trying to work it out.

  • 写回答

2条回答 默认 最新

  • doujun1495 2014-07-16 04:19
    关注

    You're trying to match HÓWL instead of Hówl..

    $data = "'us/Llane/Hówl' then some other text then 'us/Casey/Hówl' and so on";
    preg_match_all("~us/(.*?)/Hówl~", $data, $output);
    print_r($output[1]);
    

    Output

    Array
    (
        [0] => Llane
        [1] => Casey
    )
    

    Alternatively, unless you know that Hówl will always be on the right side of the forward slash I would consider using the full Letter Unicode property \p{L}. This will allow you to match accented characters as well.

    preg_match_all("~us/(.*?)/\p{L}+~u", $data, $output);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大