I'm using flatfiles to store my content for my site. Now I have no idea on how to make a function that searches for a string in the file, but only before the first occurence of an "|" (without ""). If the string isn't found before the | on the line, continue on the line below. If the string is found, delete the whole line the string is in. If no match is found, return false, else return true.
I know i'm asking for much, but I don't know how to do this myself, and haven't found any code after 1 hour searching....
Flatfile line deleteing
Moderator: General Moderators
Found code to serach for it, but how do i make it only search before the first occurence of the "|"?
Code: Select all
<?php
/// Set some variables here
$file = 'links.txt'; // the file to use
$found = false; // boolean "found" to say if out value was found ot not
$content = file($file); // read the lines of the $file file into an array
// what to look for
$find = "test"; // example
// for i is zero, if i is smaller then the size of $content then add one to i and do it again
for($i = 0; $i < count($content); $i++) {
// if the line held in $content[$i] is the line we're looking for...
if($content[$i] == $find) {
// unset it from the array (delete it)
unset($content[$i]);
// set our found variable to true
$found = true;
// and discontinue the loop because there's no need to go further
break;
}
}
// if we did find a matching line...
if($found)
{
// we want to open up the file to rewrite the content, minus the line we don't want
$fp = fopen($file, 'w');
$writestring = '';
// for each item in the $content array
foreach($content as $line)
{
// add it to our write string
$writestring .= $line;
}
// then write it to the file
fwrite($fp, $writestring);
fclose($fp);
}
?>Code: Select all
// what to look for
$find = "test"; // examplei hope i m right.. i m not very good
that makes it look for |, and not the string i want to look forDraco_03 wrote:so put | instead of test......Code: Select all
// what to look for $find = "test"; // example
i hope i m right.. i m not very good
i don't want to find |, i want to look for a matching string before the first |, when | is found, it should jump down to the next line.
ah sorry my mistake...
well youll have to add code
i m just giving you ideas cuz i'm really not good
you could explode you string into an array delimited by the |
http://ca2.php.net/strings
this is very usefull (i m goiing to grab somehting to eat)
good luck
well youll have to add code
i m just giving you ideas cuz i'm really not good
you could explode you string into an array delimited by the |
http://ca2.php.net/strings
this is very usefull (i m goiing to grab somehting to eat)
good luck