Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Aug 2009
    Posts
    84
    Thanks
    10
    Thanked 0 Times in 0 Posts

    delete line from text file

    Hi

    say i have a text file with lines

    cat
    dog
    hat
    big
    small

    and i want to remove line 2 (dog) and then move everything up so there is no blank line.

    thanks

  • #2
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    <?php

    function removeLine ($url$lineToRemove)
    {
        
    $data file_get_contents($url);
        
    $lines explode(PHP_EOL$data);
        
    $lineNo 1;
        foreach(
    $lines as $line)
        {
            
    $linesArray[$lineNo] = $line;
            
    $lineNo++;
        }
        unset(
    $linesArray[$lineToRemove]);
        return 
    implode("\n"$linesArray);
    }

    removeLine ("../dir/to/file.txt"2);

    ?>
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • Users who have thanked Phil Jackson for this post:

    alcool9999 (10-27-2009)

  • #3
    New Coder
    Join Date
    Aug 2009
    Posts
    84
    Thanks
    10
    Thanked 0 Times in 0 Posts
    so is that code complete i just pass my own vairbales?

  • #4
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    site-map.txt:

    Code:
    http://www.actwebdesigns.co.uk/
    http://www.actwebdesigns.co.uk/index.html
    http://www.actwebdesigns.co.uk/web-design-mansfield/index.html
    http://www.actwebdesigns.co.uk/web-design-mansfield/contact.php
    http://www.actwebdesigns.co.uk/web-design-mansfield/portfolio.html
    http://www.actwebdesigns.co.uk/web-design-mansfield/services.php
    http://www.actwebdesigns.co.uk/web-design-mansfield/plug-n-play-cms.html
    http://www.actwebdesigns.co.uk/web-design-mansfield/terms-and-conditions.html
    http://www.actwebdesigns.co.uk/web-design-mansfield/site-map.html


    PHP Code:

    <?php

    function removeLine ($url$lineToRemove)
    {
        
    $data file_get_contents($url);
        
    $lines explode(PHP_EOL$data);
        
    $lineNo 1;
        foreach(
    $lines as $line)
        {
            
    $linesArray[$lineNo] = $line;
            
    $lineNo++;
        }
        unset(
    $linesArray[$lineToRemove]);
        return 
    implode("\n"$linesArray);
    }

    $data removeLine ("../www.actwebdesigns.co.uk(Aug2009)/web-design-mansfield/site-map.txt"2);

    echo 
    $data

    ?>
    OUTPUT:

    Code:
    http://www.actwebdesigns.co.uk/
    http://www.actwebdesigns.co.uk/web-design-mansfield/index.html
    http://www.actwebdesigns.co.uk/web-design-mansfield/contact.php
    http://www.actwebdesigns.co.uk/web-design-mansfield/portfolio.html
    http://www.actwebdesigns.co.uk/web-design-mansfield/services.php
    http://www.actwebdesigns.co.uk/web-design-mansfield/plug-n-play-cms.html
    http://www.actwebdesigns.co.uk/web-design-mansfield/terms-and-conditions.html
    http://www.actwebdesigns.co.uk/web-design-mansfield/site-map.html
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #5
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    works fine for me on any file. so try this for me

    PHP Code:
    <?php

    function removeLine ($url$lineToRemove)
    {
        if(
    file_exists($url))
        {
            
    $data file_get_contents($url);
            
    $lines explode(PHP_EOL$data);
            
    $lineNo 1;
            foreach(
    $lines as $line)
            {
                
    $linesArray[$lineNo] = $line;
                
    $lineNo++;
            }
            unset(
    $linesArray[$lineToRemove]);
            return 
    implode("\n"$linesArray);
        }
        else
        {
            return 
    "incorrect file dir";    
        }
    }

    $data removeLine ("../www.actwebdesigns.co.uk(Aug2009)/web-design-mansfield/site-map.txt"2);

    echo 
    $data

    ?>
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #6
    New Coder
    Join Date
    Aug 2009
    Posts
    84
    Thanks
    10
    Thanked 0 Times in 0 Posts
    ok that works but it doesn't actually remove the line from the text file

  • #7
    New Coder
    Join Date
    Aug 2009
    Posts
    84
    Thanks
    10
    Thanked 0 Times in 0 Posts
    This is the code i am using. i have to find the line first.


    <?php
    $tofind="boy";
    $match="0";
    $linef="0";
    $line = count(file('remove.txt'));
    $myFile = "remove.txt";
    $fh = fopen($myFile, 'r');
    for ($i=1; $i<=$line; $i++)
    {
    $user1 = fgets($fh);
    if (trim($user1) == trim($tofind))
    {
    $match="1";
    $linef=$i;
    }
    }
    echo($linef);

    function removeLine ($url, $lineToRemove)
    {
    $data = file_get_contents($url);
    $lines = explode(PHP_EOL, $data);
    $lineNo = 1;
    foreach($lines as $line)
    {
    $linesArray[$lineNo] = $line;
    $lineNo++;
    }
    unset($linesArray[$lineToRemove]);
    return implode("\n", $linesArray);
    }

    $data = removeLine ("remove.txt", $linef);

    echo($data);

    ?>

    everything works. it echos it back without "boy" but just doesn't remove from file

  • #8
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    do ytou want it to re-write the file then? give me 5
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #9
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    I also added multi line option, seperate line numbers with comman e.g. "2, 5, 6"

    PHP Code:
    <?php

    function removeLine ($url$linesToRemove)
    {
        if(
    is_string($linesToRemove) && is_string($url))
        {
            if(
    file_exists($url))
            {
                
    $linesToRemoveArray explode(","$linesToRemove);
                
    $linesArray = array();
                
    $data file_get_contents($url);
                
    $lines explode(PHP_EOL$data);
                
    $lineNo 1;
                foreach(
    $lines as $line)
                {
                    
    $linesArray[$lineNo] = $line;
                    
    $lineNo++;
                }
                foreach(
    $linesToRemoveArray as $lineToRemove)
                {
                    @unset(
    $linesArray[trim($lineToRemove)]);    
                }
                
    $newData implode("\n"$linesArray);
                if(
    $fp = @fopen($url'w'))
                {
                    if(@
    fwrite($fp$newData))
                    {
                        
    fclose($fp);
                    }
                    else
                    {
                        die(
    "Could not write data to file");    
                    }
                }
                else
                {
                    die(
    "Could not open file");        
                }
            }
            else
            {
                die(
    "Incorrect file dir");        
            }
        }
        else
        {
            die(
    "All parameters must be string");        
        }
    }

    removeLine ("../www.actwebdesigns.co.uk(Aug2009)/web-design-mansfield/site-map.txt""2, 5");

    ?>
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #10
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    PHP Code:
    $content file("yourfilename.txt");
    unset(
    $content[$lineposition]); 
    lineposition start from 0 not 1, and all you need is to write the array to a file and add new lines,

    best regards

  • #11
    New Coder
    Join Date
    Aug 2009
    Posts
    84
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Phil that code gives me an error but i have done it like this:

    <?php
    $tofind="today";
    $match="0";
    $linef="0";
    $linefound="0";
    $line = count(file('remove.txt'));
    $myFile = "remove.txt";
    $fh = fopen($myFile, 'r');
    for ($i=1; $i<=$line; $i++)
    {
    $user1 = fgets($fh);
    $array[$i - 1] = $user1;
    $linef = $linef + 1;
    if (trim($user1) == trim($tofind))
    {
    $linefound=$i;
    }
    }
    fclose($fh);

    echo($linef);

    $fh = fopen($myFile, 'w');
    for ($i=1; $i<=$line; $i++)
    {
    if ($i == $linefound)
    {
    $match="1";
    } else {
    fwrite($fh, $array[$i - 1]);
    }
    }
    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •