PHP - 正则表达式在方括号内找到字符串

时间:2015-11-04 13:41:44

标签: php regex

我有以下示例文本

locationManager:didChangeAuthorizationStatus:

我想使用正则表达式来识别上面的字符串是否包含标题部分...如果确实如此,我需要提取其属性和方括号内的内容,即a标签和img标签

到目前为止,我能够提出

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

这似乎能够识别开头标题&#39;标签&#39;

1 个答案:

答案 0 :(得分: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"