【发布时间】:2012-03-12 10:08:29
【问题描述】:
我想在下面的 html 字符串中捕获方括号内的文本。 但是我在下面的正则表达式并没有分别得到 'image' 和 imagealt' 而是返回 'image]" alt="[imagealt' 。如果我从字符串中取出 alt="[imagealt]" ,它会按照我的预期/想要返回。
$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
)
)
【问题讨论】: