php正则表达式如何反斜杠

时间:2015-03-26 17:19:09

标签: php regex

我想制作这个正则表达式并尝试在我的PHP脚本中使用它。但它只是在我的脚本中的雷鬼建设者工作。我认为它必须与反斜杠有关,但我确实放置并杀死了很多它们而没有付出一些努力。

Link to the working regex

这是提供的php版本,但它根本无法工作:

$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);

我需要考虑让它起作用吗?

1 个答案:

答案 0 :(得分:2)

使用:

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

print_r($matches)输出:

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
        )

)

我使用较少的转义并更改了方括号中的引号。在[\\\"\\']?到非捕获变更(?:\"|')?

相关问题