2015-11-04 136 views
0

我有以下示例文本PHP - 方括號中的正則表達式查找字符串

Some HTML[caption id="attachment_146" align="alignleft" width="450" caption="Annette during 2008 WSOP"]<a href='123'><img src='xxx' /></a>[/caption]More HTML 

我想用正則表達式來確定,如果上述繩子包含標題部分......如果它確實我需要提取它的屬性和方括號內的內容,即在一個標籤和img標籤

到目前爲止,我能想出

preg_match('^\[caption(.*?)\]^', $content, $matches); 

這似乎是能夠識別打開標題「標籤」

+2

當我用Google搜索你究竟瓷磚,發現至少10個答案的方式。答案都在這裏 – swidmann

回答

0

嘗試以下操作:

$re = "/\\[caption ([^\\]]*)\\]/"; 
$str = "Some HTML[caption id=\"attachment_146\" align=\"alignleft\" width=\"450\" caption=\"Annette during 2008 WSOP\"][/caption]More HTML"; 

preg_match($re, $str, $matches); 

這將返回:

id="attachment_146" align="alignleft" width="450" caption="Annette during 2008 WSOP" 
相關問題