reading, writing and searching on text files

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
hey yall
I want to read a text file and check for a special word on it, how can
I do that? Also I want to search for a special word such as "?>" and
write something on the line that preceeds it if u see what I mean.

thanx in advance

Re: reading, writing and searching on text files

Quoted text here. Click to load it

A simple solution to your first question could go a little something like

$txtfile    =    file('text.txt');
$txtfile    =    implode('\n',$txtfile);

Then run some sort of search function on $txtfile, such as strstr() or

The answer to your second question is a little more complicated because it
would probably involve a regexp. Suppose you wanted to create a line that
contained the words "A NEW LINE" before the special word...

$txtfile    =    <<<BLOCK
foo bar baz something something more
something else SPECIALWORD

$txtfile    =    preg_replace( "/(\n.*)(SPECIALWORD)/", "\nA NEW LINE$1$2",
$txtfile );

- JP

Site Timeline