Krishna Srinivasan wrote:
Is there a PHP equivalent of the Perl Text::Context? Given a piece of
text and some search terms, the function must return a string that
contains the search words and words that are near the search terms. Is
this asking for too much? This is for a search engine I'm building.
Right now I'm using multiple nested conditions and this tends to fail
in a few cases.
Try pear.php.net for a spin. BTW, below is a basic example which might gets
you going with your current code:
function search ($words, $context, $n = 5000) {
$words = preg_split("/\s+/", $words);
$result = array();
foreach ($words as $word) {
$reg = '/(.{0,'.$n.'}'.$word.'.{0,'.$n.'})/';
if (preg_match($reg, $context, $matches)) {
$result[] = trim(
str_replace(
$word,
"<b>$word</b>",
$matches[1]
)
);
}
}
return $result;
}
print_r(search("shining sun", "the sun is shining"));
JW