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 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?