I'm searching a mysql database getting results which match the searched keyword. When the keyword is found within the search string I want to show the searcher that keyword before cutting the showed string down to 500 chars, therefore I move back from the keyword 1 char at a time until a "." is found, and make two positions after the fullstop the start of my string which is showed.
Maybe that wasn't explained well, hopefully you'll understand when you see the code...
while ($row = mysql_fetch_array($result)) {
$title = $row["title"];
$content = nl2br($row["content"]);
$begin = strpos($content, $var); //find position of keyword
while($begin != stripos($content, ".")) { //until fullstop is found
$begin--; //move positions back
}
$begin = $begin+2; //dont include full stop in displayed message
$content = substr($content, $begin, 500); //get beginning of string + 500 chars to show
echo "Title:$title<br>Content:$content";
}
The problem is that often, I don't think its always finding the fullstop, sometimes it appears to do so, otherwise it doesn't (depending on the keyword searched).
Please help me find a fix to this!