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 phrase (or x if there is only x words there) from a block of text (when I say words I mean words or numbers whatever is in the block of text)
eg
Welcome to Stack Overflow! Visit your user page to set your name and email.
if you was to search “visit” it would return:
Welcome to Stack Overflow! Visit your user page to set
the idea is to use preg_match_all in php to give me a bunch of search results showing where in the text the search phrase appears for each occurrence of the search phrase.
Thanks in advance 😀
on a sub note there may be a better way to get to my result if you feel there is please feel free to throw it in the pool as I’m not sure this is the best just the first way I thought of, to do what I need 😀
How about this:
will match five “words” (but accepting less if the text is shorter) before and after your search word (in this case
visit).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).