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 (?:\"|')?.

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

报告相同问题?