使用PHP替换txt文件中的区域

时间:2013-05-07 13:50:13

标签: php file

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

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

现在我想删除从<schedule></schedule>的数据,包括使用php网站的标签。

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

非常感谢

1 个答案:

答案 0 :(得分: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);
相关问题