preg_replace 无法在循环内工作

preg_replace not working inside loop

我正在解析一个 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.

有什么想法吗?

提前致谢。

要保存更改,您必须通过引用将其用作 &$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
}

应该可以。让我知道它是否仍然没有。

已编辑: 只需在上面的循环末尾添加 $item['description'] = $desc;