Forum Moderators: coopster
Firstly I find all records:SELECT text_field FROM table WHERE text_field like '%$text_to_find%'
You know "text_field" contains in it "text_to_find" - somewhere. Find where it is (or the first occurrence).
In this example I intend to go -100 and +100 chars.
$whereisit = strpos($text_field, $text_to_find)-100;
$whereisit = $whereisit > 0? $whereisit:0;$text_out = substr($text_field, $whereisit, 200);
I now highlight the text in its context:
$text_out = str_replace($text_to_find,"<b>$text_to_find</b>",$text_out);
And display it:
echo "<p>... <i>$text_out</i> ...</p>";
You might find better use of case insenstive functions, maybe, or do like I do and translate everything to lower case first.
Another function that can be used in these situations is str_word_count() [php.net].