I want to capture the text within the square brackets in the html string below. But the regex I have below doesn't get 'image' and imagealt' seperately but returns 'image]" alt="[imagealt' instead. If I take out the alt="[imagealt]" from the string it returns as I would expect/want.
$html = '<h2>[title]</h2>
<div class="content"><img src="[image]" alt="[imagealt]" /></div>
<div class="content">[text]</div>';
preg_match_all("^[(.*)]^",$html,$fields, PREG_PATTERN_ORDER);
echo "<pre>";
print_r($fields);
echo "</pre>";
Array
(
[0] => Array
(
[0] => [title]
[1] => [image]" alt="[imagealt]
[2] => [text]
)
[1] => Array
(
[0] => title
[1] => image]" alt="[imagealt
[2] => text
)
)
See Question&Answers more detail:os