Img与PHP正则表达式匹配的标记源 [英] Img tag src matching PHP regex

查看:51
本文介绍了Img与PHP正则表达式匹配的标记源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

src="http://3.bp.blogspot.com/-ulEY6FtwbtU/Twye18FlT4I/AAAAAAAAAEE/CHuAAgfQU2Q/s320/DSC_0045.JPG"
基本上,任何在src属性中具有某种bp.blogpot URL的内容。我有以下功能,但它只起了部分作用:

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

推荐答案

此URL接受所有BlokSpot URL并允许转义引号:

src="((?:[^"]|(?:(?<!\)(?:\\)*\"))+blogspot.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
     # 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

这篇关于Img与PHP正则表达式匹配的标记源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆