0

我需要在Maildir之后更改单词,在此示例 中为NULL一行:(单词 NULL 可以是任何单词。)

  [3]=>
  string(42) "SPAM
* ^Subject: \[SPAM\].*
Maildir/.NULL/"

我使用这段代码:

if($filecontent = file_get_contents($filename)){

    $repertoire = "SPAM";
    $tab = explode("#", trim($filecontent));
    $tab[3] = preg_replace("#Maildir/*#", 'Maildir/.'.$repertoire.'/', $tab[3]);

    var_dump($tab);

但是我有

  string(48) "SPAM
* ^Subject: \[SPAM\].*
Maildir/.NULL/.SPAM/"

我如何在Maildir/.SPAM中更改它,因为.NULL过多。

  string(48) "SPAM
* ^Subject: \[SPAM\].*
Maildir/.SPAM/"
4

1 回答 1

0

你可以这样做:

 preg_replace('~Maildir/\K\.NULL/(?=\.SPAM)~', '', $string);

或任何词:

 preg_replace('~Maildir/\K[\w.-]++/(?=\.SPAM)~', '', $string);

我已经测试过:

$string = <<<LOD
SPAM
* ^Subject: \[SPAM\].*
Maildir/.NULL/.SPAM/
LOD;
echo preg_replace('~Maildir/\K[\w.-]++/(?=\.SPAM)~', '', $string).'<br/>';
echo preg_replace('~Maildir/\K\.NULL/(?=\.SPAM)~', '', $string).'<br/>';
于 2013-05-03T21:13:55.427 回答