douyong4842 2015-03-26 17:19
浏览 62
已采纳

php正则表达式如何反斜杠

I want made this regex and tried to use it within my PHP script. But it is just working in the reggae builder not in my script. I think it have to be something with the backslashes but I really placed and killed a lot of them without some effort.

Link to the working regex

This is the provided php version, but it simply won't work:

$re = "/< *img[^>]*title= *\\\\ *[\\\"\\']?([^\\\\\\\"\\']*)[^>]*src= *\\\\ *[\\\"\\']? *data:image\\/jpeg;base64,([^\\\\\\\"\\']*)/"; 
$str = "<img style=\"\" title=\"Dies ist der Bild Titel\" src=\"data:image/jpeg;base64,/9j/4AAQ\">Hier ist Text<img title=\"Hier ist ein anderer\" src=\"data:image/jpeg;base64,/9j/4ABQ\">"; 

preg_match_all($re, $str, $matches);

What I have to consider making it work?

  • 写回答

1条回答 默认 最新

  • douhoujun9304 2015-03-26 17:44
    关注

    Using:

    $re = "/< *img[^>]*title= *\\ *(?:\"|')?([^\"']*)[^>]*src= *\\ *(?:\"|')? *data:image\/jpeg;base64,([^\"']*)[^>]/"; 
    

    print_r($matches) outputs:

    Array
    (
        [0] => Array
            (
                [0] => <img style="" title="Dies ist der Bild Titel" src="data:image/jpeg;base64,/9j/4AAQ"
                [1] => <img title="Hier ist ein anderer" src="data:image/jpeg;base64,/9j/4ABQ"
            )
    
        [1] => Array
            (
                [0] => Dies ist der Bild Titel
                [1] => Hier ist ein anderer
            )
    
        [2] => Array
            (
                [0] => /9j/4AAQ
                [1] => /9j/4ABQ
            )
    
    )
    

    I used fewer escapes and changed the quotes in square brackets thing you had goin' on [\\\"\\']? to non-capturing alternations (?:\"|')?.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?