如何删除php文件中的多行

时间:2014-05-15 01:28:28

标签: php file

我真的在寻找我想做的事,但我找不到它。

所以基本上我有一个脚本应该删除一篇文章并删除同一篇文章的摘要。所有文章的所有摘要都保存在同一个文件中。这是文件的基本格式:

<!-- START article.html --><article id="class="post"><div id="post" class="post">
    <div class="posthead">
    <div class="dater">

    <p class="day"><a href="{$URL}article.html" >10</a></p>
    <p class="monthyear"><a href="{$URL}article.html" >May 14</a></p>
    </div>

        <h2><a href="{$URL}article.html"><span style="color: #2F64A4">article</span></a></h2></div>

        <div class="entry">
    Content summary here.
    <p style="text-align: justify;"> <a href="{$URL}article.html#more" class="more-link">Read further &raquo;</a></p>
                        </div>
        <!-- <p class="postmetadata"><span class="comm"><a href="{$URL}article.html#respond" title="Comments on article.">No comments yet.</a></span></p> -->
    </div>

所以是的,所有的摘要都以这种格式存储在一个生命中(并且全部由a分隔)。我正在尝试创建一个可以删除所需摘要的脚本,但我知道我能做的就是找到声明简历开头并删除该行的行。我不知道如何删除其他行直到结束。

这是我到目前为止所做的:

if(isset($_POST['submit']) && isset($_POST['file'])) {
foreach($_POST['file'] as $file) {
    if(isset($file)) {
        if (unlink('../content/'.$file.'')) {        //This line deletes the article

           //looking for deleting the abstract of the article
           $extrait = fopen('../content/extraits.html', "r+");
           if($extrait)
           {
              while(($ligne = fgets($extrait)) !== false)
              {
                if($ligne = '<!-- START '.$file.' -->)
               {     //delete here
                     //How do I delete the other lines that come after up until the <!-- END '.$file.' --> line?
               }
              }
           }
    }
echo "Deleted the file: $file<br />"; } else {
            echo "Didn't manage to delete the file: $file<br />";
        }
    }

提前致谢!

P.S:如果你想让我知道怎么做,如果我想编辑抽象而不是删除它(做另一个函数),那么这真的很好!

1 个答案:

答案 0 :(得分:0)

在该行中搜索 <!-- END '.$file.' --> ,如果不存在,请将其删除。

$delete=false;
while(($ligne = fgets($extrait)) !== false){
    //TEST IF DELETING IS ON
    if($delete===true){
        //TEST IF <!-- END IS IN THIS LINE. IF NOT, DELETE THIS LINE
        if($strpos($ligne,'<!-- END '.$file.' -->')===false){
           //delete the line
           }
        //IF <!-- EN IS IN THIS LINE, DELETE AND STOP DELETING
        else{
           //delete this line
           $delete=false; //NOTHING MORE TO DELETE
           break;  //STOP SEARCHING
           }            
    //IF NOT YET DELETING, LOOK IF THE START OF THE ABSTRACT IS THERE AND START DELETING
    if($ligne = '<!-- START '.$file.' -->){
        //delete line here
        $delete=true;  //TURN DELETING ON
        }
    }

旁注:这通常是通过使用数据库完成的。如果你不能,考虑一种更易于管理的方式来存储你的数据并使用模板来显示html,而不是存储完整的html。

如果你存储的内容如下:

[*]filename 1|info 1|info 2|info 3
[*]filename 2|info 1|info 2|info 3
[*]filename 3|info 1|info 2|info 3

并将一个文件中的所有数据保存在一行上,您只需要在线删除它。通过这种方式,您还可以更轻松地编辑信息。