【发布时间】:2010-06-17 18:06:58
【问题描述】:
Get all text between tags with preg_match_all() or better function?的后续问题
给定以下 POST 数据:
2010-June-3
<remove>2010-June-3</remove>
2010-June-15
2010-June-16
2010-June-17
2010-June-3
2010-June-1
我只想删除 2010 年 6 月 3 日的第一个实例,但以下代码会删除所有数据。
$i = 1;
$pattern = "/<remove>(.*?)<\/remove>/";
preg_match_all($pattern, $_POST['exclude'], $matches, PREG_SET_ORDER);
if (!empty($matches)) {
foreach ($matches as $match) {
// replace first instance of excluded data
$_POST['exclude'] = str_replace($match[1], "", $_POST['exclude'], $i);
}
}
echo "<br /><br />".$_POST['exclude'];
这回响:
<remove></remove>
2010-June-15
2010-June-16
2010-June-17
2010-June-1
它应该回显:
<remove>2010-June-3</remove>
2010-June-15
2010-June-16
2010-June-17
2010-June-3
2010-June-1
【问题讨论】:
标签: php preg-match-all str-replace