2013-04-22 85 views
0

充分利用文本中的src我通過短碼將圖像添加到一個WP網站:使用的preg_match

[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]比賽。

回答

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

[圖URL = 「http://example.com/large.gif」 標題= 「我的標題」 SRC =「HTTP://例子。 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 
) 
+0

這很有用,我可以問有沒有什麼方法可以去掉模式以獲取src?我喜歡所有其他選項,但我只是爲了src。 – Ahhhhhhhhhhhhhdfgbv 2013-04-22 06:36:59

+0

我的正則表達式是爲了以隨機順序匹配屬性而構建的。如果你只需要src,你可以使用'/\[figure.* src =「(? [^」] +)「。* \]/iU'來代替 – 2013-04-22 07:06:18

+0

感謝這兩個表達式。 – Ahhhhhhhhhhhhhdfgbv 2013-04-22 07:16:21

0

試試這個

$foo = [figure src="" url"" caption=""]; 
preg_match('/src="([^"]*)"/i', $foo, $array) ; 
$finalStr = $array[0]; 
$explode = explode("=", $finalStr); 
echo $explode[1]; 
+0

沒有爲我工作。 – Ahhhhhhhhhhhhhdfgbv 2013-04-22 05:57:03

+0

哎呀,我給$ array [1]。我用$ array [0]更新了我的答案,你現在可以嘗試嗎? – 2013-04-22 06:00:09

+0

你的回答是目前給我的'src = IMG.jpg',我只需要'IMG.jpg' – Ahhhhhhhhhhhhhdfgbv 2013-04-22 06:06:10