php filter word code.. need help

Discussion in 'PHP' started by kotaro, May 6, 2009.

  1. #1
    hi. i need your help.

    content of word.txt:
    abc
    def
    ghi
    --EOF--

    this is the code


    <?php
    $word = file_get_contents("./word.txt");
    $wordarray = explode("\n",$word);
    $string = "Aabc test1 Ddef test2 ghi";
    foreach($wordarray as $output)
    {
    $string = eregi_replace($output, "", $string);
    }
    echo $string;
    ?>


    the output is:

    A test1 D test2

    BUT! i want to remove the exact word.

    i.e. in this case, only ghi to be removed.

    so the output must be

    Aabc test1 Ddef test2
     
    kotaro, May 6, 2009 IP
  2. 123addme

    123addme Active Member

    Messages:
    171
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Add space before and after word and replace by a space ?
     
    123addme, May 6, 2009 IP
  3. lutics

    lutics Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    When I run that code I get
    "Aabc test1 Ddef test2" ????

    If your still having problems try

    That also works for me.
     
    lutics, May 6, 2009 IP
  4. netwaydev

    netwaydev Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    you shouldn't be using ereg_replace, as it is considered sort of bad practice and is going to be dropped from php6 I guess. try this:

    $string = preg_replace("/\b".$output."\b/", "", $string);

    \b stands for word boundary. ;)
     
    netwaydev, May 6, 2009 IP