Hi I am trying to create a regex that will do the following
grab 5 words before the search phrase (or x if there is only x words there) and 5 words after the search
How about this:
(\S+\s+){0,5}\S*\bvisit\b\S*(\s+\S+){0,5}
will match five "words" (but accepting less if the text is shorter) before and after your 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'm defining a "word" as a sequence of non-whitespace characters, separated by at least one whitespace.
The search words should be actual words (starting and ending with an alphanumeric character).