duanpo7354 2014-05-23 01:57
浏览 119
已采纳

PHP正则表达式/搜索并替换多个文件中的多行字符串

I have several hundred .markdown files that I need to loop through and replace the following multiline strings:

---
---

I currently have the following code:

foreach (glob("*.markdown") as $filename)
{
    $file = file_get_contents($filename);
    file_put_contents($filename, preg_replace("/regexhere/","replacement",$file));
}

My question is, which regex do I need to remove the multi line strings in every file.

Thanks

  • 写回答

2条回答 默认 最新

  • dongleiqiao2107 2014-05-23 02:21
    关注

    this can be done faster with str_replace(), like so:

    <?php
    echo "<pre>";
    
    $file="my file
    is
    this
    ---
    ---
    goats";
    
    echo str_replace("---
    ---
    ",'',$file);
    

    which returns:

    my file
    is
    this
    goats
    

    Live Demo: http://codepad.viper-7.com/p1Bant

    line breaks can be or depending on os\software

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料