【问题标题】:Replace region in txt file using PHP [closed]使用 PHP 替换 txt 文件中的区域 [关闭]
【发布时间】:2013-05-07 13:50:12
【问题描述】:

我有一个名为 Notifications.txt 的文本文件。它包含某些内容组。每个组都以<grouptitle> 标签开始,以</grouptitle> 标签结束{grouptitle changes> 例如这里是一个示例内容

<schedule> New schedule for Mathematics 307 has been emailed to students </schedule>
<convocation> The convocation of 34th batch will be on may 3rd </convocation

现在我想删除从&lt;schedule&gt;&lt;/schedule&gt;的数据,包括使用php网站的标签。

我该怎么做?而且我的时间有点短,如果你们也可以发布示例代码,我将不胜感激

非常感谢

【问题讨论】:

  • 这可能对stackoverflow.com/a/3004080/973578有帮助
  • 这听起来像 XML。您可以使用 SimpleXML 之类的文件解析文件,也可以使用 preg_replace 删除内容。

标签: php file


【解决方案1】:

您可以通过用正则表达式替换标签及其内容来做到这一点:

$contents = file_get_contents('path/to/file/Notifications.txt');
$contents = preg_replace("!<schedule>.*?</schedule>\r?\n!s", '', $contents);
file_put_contents('path/to/file/Notifications.txt', $contents);

【讨论】:

  • @7ochemThanks man.. 这就像一个魅力。:) 荣誉
猜你喜欢
  • 1970-01-01
  • 2013-10-09
  • 2021-07-28
  • 1970-01-01
  • 1970-01-01
  • 2013-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多