doushenyi9104 2013-05-02 13:45
浏览 25

使用php更改一行文本文件中的单词

I need to change the word just after Maildir, in this exemple NULL of a line : (the word NULL can be any word.)

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

I use this code :

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

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

    var_dump($tab);

But i have

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

How can i change that in Maildir/.SPAM because .NULL is in excess.

  string(48) "SPAM
* ^Subject: \[SPAM\].*
Maildir/.SPAM/"
  • 写回答

1条回答 默认 最新

  • dongxiaowei_1234 2013-05-03 21:13
    关注

    You can do like this:

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

    or for any word:

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

    I have tested with:

    $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/>';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了