FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP - Find and Delete





n0obie4life
Code:
$string = 'string';
$replace = '';
str_replace($string, $replace, $file);


Apprantly, that inside a fopen doesn't want to work :\.

So, is there actually a way to "Find" a particular string, then delete the whole line?

In the example above, the file would have looked like is

Code:
one
nice
string
to
delete
BlackSkad
The code below should work to remove lines with the pattern `String`, but I haven't tested it yet. I documented it a lot so you can easily understand it.
Code:
<?php
//file() opens a file and puts the content in an array
//each element of the array is a line
$fileText = file('yourFile.txt');

//an empty array to contain all the new lines (ie without the 'String'-pattern)
$newFile = null;

//takes every line of the file
foreach ($fileText as $line) {
   // checks the line for matches of 'String'
   if (preg_match('String',$line) == 0) {
      //if no matches were found, put it in the new file
       $newFile[] = $line;
   }
}

//just output to see if everything is done well disable or remove if unneeded
echo ("<pre>");
print_r($newFile);
echo ("</pre>");
?>


Greatz,
BlackSkad
yjwong
Anyway if you are doing a word filter, you can use stri_replace()
n0obie4life
No, not a word filter. To take a line out of the file Wink.

But that doesn't remove the line, BlackSkad.

Btw, is it possible to do it directly in the file instead of having to create a new file?

I'm thinking something like

Code:
$string = 'string';
$replace = '';
str_replace( $string, $replace, $file );
dandelion
Try to use the file_get_contents() function instead of the file() one.
Then search for "string\n" instead of 'string' (please note on double quotas).
Code:
<?php
    $file = file_get_contents('file.txt');
    $string = "string\n";
    $replace = '';
    str_replace($string, $replace, $file);
?>
BlackSkad
I don't think you can work with an fopen to accomplish this. But I found what was wrong. I needed to use a regular expression, instead of just 'String'.
Now it checks for every match of string (case insensitive), so also a line with 'strings' will be removed. To avoid this, use this regular expression with preg_match instead of the one used below:
Code:
'#(.*?) string (.*?)#si'


The regular expression was added, as well as some lines to write them back to the same file again. Now, I'm sure it works. It is tested on the little example-file you gave. I tested it because I'm not a crack at regular expressions Wink

Code:
<?php
//file() opens a file and puts the content in an array
//each element of the array is a line
$fileText = file('test.txt');

//an empty array to contain all the new lines (ie without the 'String'-pattern)
$newFile = null;

//takes every line of the file
foreach ($fileText as $line) {
   // checks the line for matches of 'String'
   if (preg_match('#(.*?)String(.*?)#si',$line) == 0) {
      //if no matches were found, put it in the new file
       $newFile[] = $line;
   }
}

//open the file again
if ( $fh = fopen('test.txt', 'w') ){
   //put all lines back in one string, and write them to the file again
   fwrite ($fh, implode("\n", $newFile) );
   //close the file
   fclose($fh);
} else {
   //error-handling
   die('An error occured while opening the file');
}
?>


Here you are
Greatz,
BlackSkad

@Dandelion: Your part only works with only 1 word on a line. Mine can has as many words on a line as you wish to...
Related topics
Better AD "showing"
PHP Help Needed
confused on phpBB
gallery
Content Management System Wanted
image at the end of the thread
PHP problem...
Upload Script (Read the 3rd post)
Cash Mod
Cash Mod Script Need Help!
FriHost do you use banner rotatator
tweaking wordpress
suddenly a php error on my website
possible with query? or must use PHP organize this
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.