Greg:
here is a commented sample that will produce the results you described. it is written with PHP 4.0.5 or greater in mind, as it uses the "array_search()" function, which is not found in pre 4.0.5. If you need to be compatible with earlier versions, the url to the manual is embedded in the comments, and there are examples of work arounds for this function on that page.
this could probably be done better, or more efficiently, but it does work.
hth,
:dusty
<?php
/* set up some variables for ease of use */
// number of words to surround search term with
$pWrd = 4;
// size (# of chars) to trim text string on both sides of term,
// to avoid creating *huge* array in event of a very long field.
// be careful not to set this too low (i.e. leave enough room to
// practically fit the number of words set in the previous var),
// or your output will be truncated in the middle of words.
$tLen = 100;
// text (formatting) to prepend to search term in output
$pTxt = "<span style=\"background: #CCFFCC\"><b><i>";
// text (formatting) to append to search term in output
$aTxt = "</i></b></span>";
// would normally be user input, search term
$term = "hope";
// would normally be supplied from the text in the database
$field = "some filler text. hello greg, i hope this solves your problem. some filler text.";
/* end variable set up */
/* optional routine - will save time & memory if $field is very large */
// find position of search term (spaces on both sides of $term to find whole word only)
// now position of "h" in "hope" will be stored in $tpos
$tpos = strpos($field, " $term ");
// now trim down the string, to $tLen chars on each side of the search term
// get start position. if initial position of string is greater than $tLen,
// we back up $tLen chars and start there. if <= $tLen, we start at 0.
$startPos = ($tpos > $tLen) ? ($tpos - $tLen) : 0;
echo "startPos = $startPos<br>";
// set numeric length of new string. this is the lesser of $tLen or $tpos
// chars in front, and $tLen chars behind the search term, plus the length
// of the term itself.
$trimLen = min($tLen,$tpos) + strlen($term) + $tLen;
// get the chopped field via substr()
$trimField = substr($field,$startPos,$trimLen);
// place new value into $field variable.
$field = $trimField;
/* end optional trimming routine */
// seperate field into array of individual words
$words = explode(" ",$field);
// search for term in array (php 4.0.5+ only)
// if you need compatibility with older php, check out the manual,
// http://php.net/manual/en/function.array-search.php, for alternate
// solutions to the "array_search()" function.
$termIdx = array_search($term,$words);
// go $pWrd words back or to beginning of array, whichever is first
$startAt = max(($termIdx - $pWrd),0);
// go $pWrd words fwd or to end of array, whichever is first
$sliceLen = min(($termIdx + $pWrd + 1),count($words)) - $startAt;
// make new array with only words needed
$outWords = array_slice($words,$startAt,$sliceLen);
// implode new array into a string
$output = implode(" ",$outWords);
// add formatting to search term & output results
echo eregi_replace(" $term "," $pTxt$term$aTxt ",$output);
?>