I've been working with this one for a while, and am stumped. I would assume there's a way to do it, but I sure don't know how. Knowing my luck, its probably simple, too.

I have a search engine I'm building for our site, and it is coming along nicely, with one exception. My script needs to find a matching word in the story body, which is easy.

However, it also needs to extract more than just the matching word. It also needs to take a certain amount of words to the left and right of the matching word.

The idea is to extract a portion of the story surrounding the matched word, so it will show the context in which the word was used.

Example story body:
"Php regular expression matching is awesome but sometimes a bit difficult to construct."

Lets say my matching word was 'sometimes' and that I need 2 words to each side. I would need to extract 'awesome but sometimes a bit' from the story.

If anyone could help me with this, that would be great... I'm pulling my hair out at this point.

Thanks a bunch...

    $string = "Php regular expression matching is awesome but sometimes a bit difficult to construct.";
    $word = "sometimes";
    
    preg_match("/(([\w\d]+\s){2})?" . $word . "((\s[\w\d]+){2})?/", $string, $match);
    
    print_r($match);
    
    

    this will grab 2 words to the left and right of the word you want to match, it is also written so if the word is the first or last word in the string, it wont fail, but just grab to two words before or after it. if you want to grab more than 2 words, just change {2} to {x}.

      Try that code with the word "regular", or put a comma between the words "but" and "sometimes", and you have problems.

      Slightly modified to take care of those two cases. I'm sure I'm still forgetting something else though.

      $string = "Php regular expression matching is awesome, but sometimes a bit difficult to construct.";
      $word = "sometimes";
      
      preg_match("/(([\w\d]+[^\w\d]+){1,2})?" . $word . "(([^\w\d]+[\w\d]+){1,2})?/", $string, $match);
      
      print_r($match);
      

        Thanks all... I will try it in the next few days and I'll let you all know if it works.

          5 days later

          The regex:

          preg_match("/(([\w\d]+[\w\d]+){1,2})?" . $word . "(([\w\d]+[\w\d]+){1,2})?/", $string, $match);

          doesn't work. It doesn't match anything.

            Did you try it with the example code given? If not, what word and string did you use?

            I can't think of any version of php it wouldn't work with, but just in case, what version are you using? I just tested it with 5.0.3, 4.3.10, and 4.2.2 with no problems.

              I take it back... I had to add to make the search case-insensitive, and it works great.

              Thank you again.

                9 months later

                I was searching for this 2 for some time, it works great.
                Though when you search a word, but the word is more than once in the string. He returns only the word. But I would like that he does the same thing but only for the first time he meets the word.
                How do you do this?

                Thx

                  Write a Reply...