Enter a search term check the script type and click submit. To search for a general category for example to find all results for scripts working with text files or file manipulation enter file or curl. Same for directory, sql etc. Otherwise use specific terms.
Here is the script you requested.
scripts/search-delete-file-line.txt: 1783 bytes
delete a line entry from a flat file using preg match
rewrite data to file that does not match searc
h
delete file text database flatfile
<?php
$array = array ("ball","bat","glove","referee","s
trike","home run");
$lines=file('file.txt');
unset ($array[3]); // remove 'referee'
$
array = array_values ($array);
echo '<pre>', print_r($array, true), '</pre>';
?>
<?php
$key =
"search";
//load file into $fc array
$fc=file("some.txt");
//open same file and use "w" to clear
file
$f=fopen("some.txt","w");
//loop through array using foreach
foreach($fc as $line)
{
if (!strstr($line,$key)) //look for $key in each line
fputs($f,$line); //place $line bac
k in file
}
fclose($f);
?>
<?php
$datafile = 'datafile.txt';
if (isset($_GET['id']) and i
s_numeric($_GET['id']) and ($_GET['id'] > 0)) {
$newfile = null;
$id = $_GET['id'];
$fh = fope
n($datafile, "r") or die("cannot open file for reading");
if (is_file($datafile) and $fh) {
whil
e (!feof($fh)) {
$buffer = fgets($fh, 4096);
//rewites the whole file back that does not match id
if (! preg_match("/^$id\|\|.*/",$buffer)) {
$newfile .= $buffer;
}
}
}
fclose($fh);
$f
h = fopen($datafile, "w") or die("cannot open file for writing");
if (fwrite($fh, $newfile) === FA
LSE) { die("Cannot write to $datafile"); }
fclose($fh);
// check it
$contents = file_get_conte
nts($datafile);
echo "<pre>$contents</pre>";
}
else {
echo "<p><a href=\"delete_record.php?id
=4\">delete record 4</a></p>";
$contents = file_get_contents($datafile);
echo "<pre>$contents</
pre>";
}
?>
have the unique identifier first in the list/line of fields
then use below matches |
|5(field data)||
if (! preg_match("/^[^\|]{2}?\|\|$id\|\|.*/",$buffer)) {
// etc.
Post a comment
No comments yet