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


Img tag src matching PHP regex

我正在尝试匹配src="URL"标签,如下所示:

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

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

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

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

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