【问题标题】:PHP Wordpress Shortcode Regex embedded in html嵌入在 html 中的 PHP Wordpress 简码正则表达式
【发布时间】:2013-11-15 19:09:44
【问题描述】:

我正在尝试使用 get_the_content() 从 wordpress 帖子中解析出图像。

它返回一堆 html 和多个灯箱简码:

<ul>
    <li>testing1</li>
    <li>egfgf</li>
</li>
<p>Here!</p>
[lightbox link="http://www.test.com/photo1.jpg" width="150" align="none" title="photo 1" frame="true" icon="image"]
[lightbox link="http://www.test.com/photo2.jpg" width="150" align="none" title="photo 2" frame="true" icon="image"]
[lightbox link="http://www.test.com/photo5.jpg" width="150" align="none" title="photo 5" frame="true" icon="image"]

html 并不总是像上面那样,可以是任何 html 变体。我的问题是如何使用正则表达式模式从所有灯箱短代码中获取 link 值?

期望的输出:

Array
(
[0] => Array
    (
        [0] => http://www.test.com/photo1.jpg
        [1] => http://www.test.com/photo2.jpg
        [2] => http://www.test.com/photo5.jpg
    )
)

我尝试使用的模式:

preg_match_all('/(?<![^"])\S+\.[^"]+/', $text, $matches);
print_r($matches);

这仅适用于灯箱文本,但当我添加 html 时它不起作用。

为什么我的正则表达式可以在这个站点http://regex101.com/r/eE6fU9 上工作,但不能在 php 中工作?

【问题讨论】:

    标签: php regex wordpress


    【解决方案1】:

    你可以使用下面的

    preg_match_all('/\[lightbox[^\]]*link="([^"]*)"[^\]]*\]/i', $text, $matches);
    print_r($matches[1]);
    

    demo

    【讨论】:

    • 我很难从数组中获取值。您的正则表达式 100% 有效。谢谢!
    猜你喜欢
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多