Img标签src匹配PHP正则表达式

时间:2012-02-03 00:42:59

标签: php regex preg-match

我试图匹配src ="网址"标签如下:

src="http://3.bp.blogspot.com/-ulEY6FtwbtU/Twye18FlT4I/AAAAAAAAAEE/CHuAAgfQU2Q/s320/DSC_0045.JPG"

基本上,任何在src属性中都有一些bp.blogspot URL的东西。我有以下内容,但它只是部分工作:

preg_match('/src=\"(.*)blogspot(.*)\"/', $content, $matches);

1 个答案:

答案 0 :(得分:3)

这个接受所有blogspot网址并允许转义引号:

src="((?:[^"]|(?:(?<!\\)(?:\\\\)*\\"))+\bblogspot\.com/(?:[^"]|(?:(?<!\\)(?:\\\\)*\\"))+)"

捕获URL以匹配组1。

您需要在\中使用额外的/(每次出现!)来逃避\preg_match(…)

说明:

src=" # needle 1
( # start of capture group
    (?: # start of anonymous group
        [^"] # non-quote chars
        | # or:
        (?:(?<!\\)(?:\\\\)*\\") # escaped chars
    )+ # end of anonymous group
    \b # start of word (word boundary)
    blogspot\.com/ # needle 2
    (?: # start of anonymous group
        [^"] # non-quote chars
        | # or:
        (?:(?<!\\)(?:\\\\)*\\") # escaped chars
    )+ # end of anonymous group
    ) # end of capture group
" # needle 3