PHP regular expression for a collection of words around a search phrase

Hi, I am trying to create a regex that will do the following

take 5 words before the search phrase (or x, if there are only x words) and 5 words after the search phrase (or x, if there are only x words) from a block of text (when I say words I mean words or numbers, which are in the block of text)

eg,

Welcome to stack overflow! Visit your user page to provide your name and email address.

if you were to look for a β€œvisit,” he will return: Welcome to the stack overflow! Visit your user page to install

the idea is to use preg_match_all in php to give me a bunch of search results showing where the search phrase appears in the text for each occurrence of the search phrase.

Thanks in advance: D

on an extra note, there may be a better way to get to my result, if you feel that there is, please feel free to throw it in the pool, as I'm not sure if this is the best only first way I thought of doing what I need: D

+4
source share
2 answers

How about this:

(\S+\s+){0,5}\S*\bvisit\b\S*(\s+\S+){0,5} 

will correspond to five β€œwords” (but less if the text is shorter) before and after the search word (in this case visit ).

 preg_match_all( '/(\S+\s+){0,5} # Match five (or less) "words" \S* # Match (if present) punctuation before the search term \b # Assert position at the start of a word visit # Match the search term \b # Assert position at the end of a word \S* # Match (if present) punctuation after the search term (\s+\S+){0,5} # Match five (or less) "words" /ix', $subject, $result, PREG_PATTERN_ORDER); $result = $result[0]; 

I define a β€œword” as a sequence of characters without spaces, separated by at least one space.

Search words must be valid words (beginning and ending with an alphanumeric character).

+8
source

You can do the following (this bit of computation is heavy, so it will not be effective for very long lines):

 <?php $phrase = "Welcome to Stack Overflow! Visit your user page to set your name and email."; $keyword = "Visit"; $lcWords = preg_split("/\s/", strtolower($phrase)); $words = preg_split("/\s/", $phrase); $wordCount = 5; $position = array_search(strtolower($keyword), $lcWords); $indexBegin = max(array($position - $wordCount, 0)); $len = min(array(count($words), $position - $indexBegin + $wordCount + 1)); echo join(" ", array_slice($words, $indexBegin, $len)); //prints: Welcome to Stack Overflow! Visit your user page to set 

Codepad example here

+1
source

All Articles