c0dehunter
c0dehunter

Reputation: 6150

RegExp to Get N words before and after

I want to get the "context" of a given search string. For example, for search string myself in the following line

Me, my dog and myself are going on a vacation.

I want to get dog and myself are going for N=2. So 2 words before match and 2 after.


Currently I match whole lines like this:

$lines = file($file->getFilename());
$lines = preg_grep('/'.$_POST['query'].'/', $lines);

Upvotes: 7

Views: 2948

Answers (1)

CSᵠ
CSᵠ

Reputation: 10169

preg_grep() is supposed to act like that, but it sounds like you would need preg_match() and in case you can have multiple instances of searched word in the text and want to find all of them preg_match_all()

The RegEx you're looking for is: (?:[^ ]+ ){0,2}myself(?: [^ ]+){0,2} Explained demo here: http://regex101.com/r/pB3eW0

I designed it to match 2 words before and after if it can otherwise 1 word or even none.

The code allowing a variable N could look like this:

$fileData=file_get_contents($file->getFilename());
$n=2;
$query='myself';
preg_match_all('/(?:[^ ]+ ){0,'.$n.'}'.$query.'(?: [^ ]+){0,'.$n.'}/i',$fileData,$matches);
print_r($matches);

Remember to validate and escape user input and not just use it in functions as given!

Upvotes: 10

Related Questions