0

我正在通过简码将图像添加到 WP 站点:

[figure src="" url"" caption=""]

其中src是图像源,url是指向更大图像的链接(如果需要),caption是标题。

我试图src从上面的代码中得到它:

$pattern = '/<img[^>]*src=\"?(?<src>[^\"]*)\"?[^>]*>/im';
preg_match( $pattern, $html, $matches ); 
if($matches['src']) {
    return $matches['src'];
}

但我试图弄清楚如何获得[figure]比赛。

4

2 回答 2

1
/\[figure(( src="(?<src>[^"]+)")?|( url="(?<url>[^"]+)")?|( caption="(?<caption>[^"]+)")?)*\]/i

[图 url="http://example.com/large.gif" 标题="我的标题" src="http://example.com/figure.gif"]

Array
(
    [0] => [figure url="http://example.com/large.gif" caption="my caption" src="http://example.com/figure.gif"]
    [1] => 
    [2] =>  src="http://example.com/figure.gif"
    [src] => http://example.com/figure.gif
    [3] => http://example.com/figure.gif
    [4] =>  url="http://example.com/large.gif"
    [url] => http://example.com/large.gif
    [5] => http://example.com/large.gif
    [6] =>  caption="my caption"
    [caption] =>  my caption
    [7] => my caption
)
于 2013-04-22T06:25:04.273 回答
0

尝试这个

$foo = [figure src="" url"" caption=""];
preg_match( '/src="([^"]*)"/i', $foo, $array ) ;
$finalStr = $array[0];
$explode = explode("=", $finalStr);
echo $explode[1];
于 2013-04-22T05:50:29.380 回答