Flatfile line deleteing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Flatfile line deleteing

Post 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....
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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);
}

?>
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post 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 :)
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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.
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post 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 :)
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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
Post Reply