0
一个后续问题要Get all text between tags with preg_match_all() or better function?str_replace函数比赛只一审
考虑下列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年六月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
克里,你可能想为'preg_quote'提供'$ delimiter'参数,以防万一...以及实际调用'preg_replace'。 – salathe 2010-06-17 18:14:05
好点 - 谢谢。我觉得很有趣/很伤心,我没有用preg_replace – 2010-06-17 18:25:02
代替代码,就像冠军一样工作。谢谢! – kylex 2010-06-17 18:45:54