reading, writing and searching on text files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kindermaxiz@yahoo.com

    reading, writing and searching on text files

    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
    Pat
  • kingofkolt

    #2
    Re: reading, writing and searching on text files

    <kindermaxiz@ya hoo.com> wrote in message
    news:39615087.0 408261935.17cf1 3f8@posting.goo gle.com...[color=blue]
    > 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
    > Pat[/color]

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

    <?php
    $txtfile = file('text.txt' );
    $txtfile = implode('\n',$t xtfile);
    ?>

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

    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...

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

    $txtfile = preg_replace( "/(\n.*)(SPECIALW ORD)/", "\nA NEW LINE$1$2",
    $txtfile );
    ?>

    - JP


    Comment

    Working...