This question already has an answer here:
I have the following sample text
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
I would like to use regex to identify if the above piece of string contains the caption part ... and if it does I need to extract its attributes and the content within the square brackets i.e. the a tag and img tag
So far I was able to come up with
preg_match('^\[caption(.*?)\]^', $content, $matches);
Which seems to be able to identify the opening caption 'tag'
</div>
Try the following:
$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);
This would return:
id="attachment_146" align="alignleft" width="450" caption="Annette during 2008 WSOP"