Page is a not externally linkable
Globetrotter - 3:27 pm on Oct 2, 2012 (gmt 0)
I would like to find x number of words before and after a given keyword, to make it more visible on the webpage.
So if you have an example text from a database: “PHP is presently the most popular scripting language in use on the Internet. You are able to code almost anything with it.”
If the keyword is code and I would like to find 4 words before the keyword and 1 after the keyword I would like to find “You are able to code almost”. But if html code (e.g. <strong> or </strong> ) or a punctuation mark is found (!.?’”) it need to stop matching.
So if I would like to find 5 words before the keyword and 3 words after the keyword the result i would like to have is: "You are able to code".
After searching all day I’ve come really close.
$strResult = "PHP is presently the most popular scripting language in use on the Internet. You are able to code <strong>almost</strong> anything with it.";
$strPattern = "#(?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,5}\b(code)\b(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,3}#iu";
preg_match($strPattern, $strResult, $arrMatch);
var_dump($arrMatch);
The only problem I’m not able to tackle is to let the matching stop when there is html(syntax) or a punctuation mark in the text I’m trying to match.
Any idea how to solve this?