preg_replace在循环内部不起作用

时间:2017-02-19 12:35:57

标签: php regex rss preg-replace

我正在解析RSS提要,我正在尝试删除其中一个项目的特定位,因此不会显示。

<pre>M /WITHHELD/WITHHELD/dist/gameserver/data/html-en/quests/_419_GetaPet/419_q1.htm M /WITHHELD/WITHHELD/dist/gameserver/data/html-en/quests/_419_GetaPet/419_q10.htm M /WITHHELD/WITHHELD/dist/gameserver/data/html-en/quests/_419_GetaPet/419_q11.htm M /WITHHELD/WITHHELD/dist/gameserver/data/html-en/quests/_419_GetaPet/419_q12.htm M /WITHHELD/WITHHELD/dist/gameserver/data/html-en/quests/_419_GetaPet/419_q13.htm M /WITHHELD/WITHHELD/dist/gameserver/data/html-en/quests/_419_GetaPet/419_q14.htm M /WITHHELD/WITHHELD/dist/gameserver/data/html-en/quests/_419_GetaPet/419_q15.htm M /WITHHELD/WITHHELD/dist/gameserver/data/html-en/quests/_419_GetaPet/419_q2.htm M /WITHHELD/WITHHELD/dist/gameserver/data/html-en/quests/_419_GetaPet/419_q3.htm M /WITHHELD/WITHHELD/dist/gameserver/data/html-en/quests/_419_GetaPet/419_q4.htm M /WITHHELD/WITHHELD/dist/gameserver/data/html-en/quests/_419_GetaPet/419_q5.htm M /WITHHELD/WITHHELD/dist/gameserver/data/html-en/quests/_419_GetaPet/419_q6.htm M /WITHHELD/WITHHELD/dist/gameserver/data/html-en/quests/_419_GetaPet/419_q7.htm M /WITHHELD/WITHHELD/dist/gameserver/data/html-en/quests/_419_GetaPet/419_q8.htm M /WITHHELD/WITHHELD/dist/gameserver/data/html-en/quests/_419_GetaPet/419_q9.htm M /WITHHELD/WITHHELD/dist/gameserver/data/scripts/quests/_128_PailakaSongofIceandFire.java M /WITHHELD/WITHHELD/dist/gameserver/data/scripts/quests/_129_PailakaDevilsLegacy.java M /WITHHELD/WITHHELD/dist/gameserver/data/scripts/quests/_144_PailakaInjuredDragon.java M /WITHHELD/WITHHELD/dist/gameserver/data/scripts/quests/_311_ExpulsionOfEvilSpirits.java M /WITHHELD/WITHHELD/dist/gameserver/data/scripts/quests/_312_TakeAdvantageOfTheCrisis.java M /WITHHELD/WITHHELD/dist/gameserver/data/scripts/quests/_642_APowerfulPrimevalCreature.java M /WITHHELD/WITHHELD/dist/gameserver/data/scripts/quests/_643_RiseAndFallOfTheElrokiTribe.java M /WITHHELD/WITHHELD/dist/gameserver/data/stats/items/15400-15499.xml M /WITHHELD/WITHHELD/dist/gameserver/data/stats/items/700-799.xml M /WITHHELD/WITHHELD/dist/gameserver/data/stats/npc/22600-22699.xml</pre>

基本上我想删除 <pre> 标签以及它们之间的任何内容。

我使用以下 preg_replace

preg_replace("/(<pre>.*<\/pre>)/", "", $str);

确实有效当我的字符串包含我要删除的位作为静态变量时,它不会我尝试在RSS输出循环中替换它

    $i = 1;

    foreach($rs['items'] as &$item) {
    $arr = array($i);
    $a = array_fill_keys($arr, (string)$item['description']);
    $desc = (string)$a[$i];
    $desc = preg_replace("/(<pre>.*<\/pre>)/", "", $desc);

    echo "\t<li><a href=\"$item[link]\">(".$i.")".$item['title']."</a><br />";
    echo $desc;
    echo "</li>\n";
    $i++;
    }

你可以用上面那段写得不好的代码告诉我,我完全失去了,并尝试了很多方法来完成这项工作。

描述($ desc)节点保持不变,即使我尝试将整个输出保存为变量($ htm),然后运行preg_replace。

有什么想法吗?

提前谢谢。

1 个答案:

答案 0 :(得分:0)

要保存更改,您必须将其作为&amp; $ item 而不是 $ item 通过引用传递:

foreach($rs['items'] as &$item) //Pass by reference using "&"
{
 $arr = array($i);
 $a = array_fill_keys($arr, (string)$item['description']);
 $desc = (string)$a[$i];
 $desc = preg_replace("/(<pre>.*<\/pre>)/", "", $desc);

 $htm .= "\t<li><a href=\"$item[link]\">(".$i.")".$item['title']."</a><br />";
 $htm .= replace($desc);
 $htm .= "</li>\n";
 $i++;
 $item['description'] = $desc; //Add this
}

那应该有用。 Lemme知道它是否仍然没有。

<强>编辑: 只需在循环结束时添加 $ item [&#39; description&#39;] = $ desc; ,如上所述。