php - Preg Match word and period before and after -


i have preg match working find instances of word title or loan, me modify instances of word, plus words before , after, . being delimiter stop.

preg_match_all('#\b(title|loan)\b#',$html, $matches); 

either that, or 10 words before , 10 words after word im looking for.

thank you

preg_match_all('#(?<pre>\w+) (title|loan) (?<post>\w+)#',$html, $matches); 

will capture words before , after title|loan if separated spaces. easy enough tweak if need more flexible boundaries between words.

you can access matches by:

foreach ($matches $match) {   echo $match['pre'];   echo $match['post']; } 

to match between periods (sentences) contain title|loan, can this:

preg_match_all('#[^.]*(title|loan)[^.]*#', $html, $matches); 

this match characters not periods before , after title|loan.


Comments