【问题标题】:img url by id bbcodeimg url by id bbcode
【发布时间】:2017-07-08 19:50:48
【问题描述】:

我想通过特定 ID 从 src 属性中获取链接

例如我有以下 bbcode 标签:

[img alt="" src="http://google.com" id="image"]
[img src="http://google.com" id="image2" alt=""]
[img id="image3" src="http://google.com" alt=""]

我试过\[img(?:[^]]+src="(.+?)"[^]]+(?:id|class)="image"|[^]]+(?:id|class)="image"[^]]+src="(.+?)")

但是对于第二张图片它不起作用

https://regex101.com/r/7owPlM/3

【问题讨论】:

  • 删除“单行”标志。
  • @horcrux 是的,这就是问题所在,因为我有一个单行文本......也许我可以在每个 ] 之后添加一个
    吗?
  • 或者只使用[^]]而不是两个.
  • @chris85 我有 BBCodes
  • @horcrux 我不明白.. 嗯

标签: php regex bbcode


【解决方案1】:

你可以试试这样的(https://regex101.com/r/SDYM7u/3):

\[img\s(?:.*?src="(.*?)".*?)?id="image"(?:.*?src="(.*?)".*?)?[^\]]*]

使用标志 g 和 m。唯一的缺点是在这种情况下,您需要同时检查捕获组 1 和 2 才能找到您的 url。当 src 在 id 属性之前,则匹配存储在第 1 组中,但当 id 属性在 src 之前,则 url 存储在第 2 组中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多