Text::Context in PHP

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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.

Thanks in advance.

Krishna Srinivasan.

Re: Text::Context in PHP

Krishna Srinivasan wrote:
Quoted text here. Click to load it

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 = '/(.'.$word.'.)/';
          if (preg_match($reg, $context, $matches)) {
               $result[] = trim(
     return $result;

print_r(search("shining sun", "the sun is shining"));


Site Timeline