...

View Full Version : delete line from text file



alcool9999
10-25-2009, 06:10 PM
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

Phil Jackson
10-25-2009, 06:21 PM
<?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);

?>

alcool9999
10-25-2009, 06:27 PM
so is that code complete i just pass my own vairbales?

Phil Jackson
10-25-2009, 06:43 PM
site-map.txt:



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

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:


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

Phil Jackson
10-25-2009, 06:54 PM
works fine for me on any file. so try this for me


<?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

?>

alcool9999
10-25-2009, 07:15 PM
ok that works but it doesn't actually remove the line from the text file

alcool9999
10-25-2009, 07:28 PM
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

Phil Jackson
10-25-2009, 09:02 PM
do ytou want it to re-write the file then? give me 5

Phil Jackson
10-25-2009, 09:14 PM
I also added multi line option, seperate line numbers with comman e.g. "2, 5, 6"



<?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");

?>

oesxyl
10-25-2009, 11:19 PM
$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

alcool9999
10-26-2009, 12:12 PM
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]);
}
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum