1

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'

1
  • 2
    By the way I googled exactly your tile, and found at least 10 answers to that. The answers are all here at SO Commented Nov 4, 2015 at 13:44

1 Answer 1

0

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"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.