Page 1 of 1

Flatfile line deleteing

Posted: Wed Dec 17, 2003 10:26 am
by vigge89
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....

Posted: Wed Dec 17, 2003 10:34 am
by vigge89
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);
}

?>

Posted: Wed Dec 17, 2003 10:38 am
by Draco_03

Code: Select all

// what to look for
$find = "test"; // example
so put | instead of test......
i hope i m right.. i m not very good :)

Posted: Wed Dec 17, 2003 10:43 am
by vigge89
Draco_03 wrote:

Code: Select all

// what to look for
$find = "test"; // example
so put | instead of test......
i hope i m right.. i m not very good :)
that makes it look for |, and not the string i want to look for :P
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.

Posted: Wed Dec 17, 2003 10:49 am
by Draco_03
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 :)

Posted: Wed Dec 17, 2003 11:04 am
by vigge89
yeah, that could be an idea, but aren't there any easier way?
just wanna be sure before i start coding a big clumsy function :D