PHP - 方括号中的正则表达式查找字符串
问题描述:
我有以下示例文本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);
这似乎是能够识别打开标题“标签”
答
尝试以下操作:
$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"
当我用Google搜索你究竟瓷砖,发现至少10个答案的方式。答案都在这里 – swidmann