Reputation: 401
I am developing a web application using PHP and MySQL. In my application I create a part for searching paragraph using FULL-TEXT search from mysql. e.g: In my database I have a paragraph that contains 1200 characters. I performed a query "test" and the word "test" in my paragraph is at middle location. I want to show only 200 characters with matched area. How can I do this with PHP?
Upvotes: 0
Views: 792
Reputation: 18558
use preg_match("#(.{0,100}$query.{0,100})#", $Data from Database, $matches);
This will assign the variable $matches with an array, element [0] should contain the word and up to 100 letters either side of the word/phrase searched for.
Upvotes: 3
Reputation: 12059
Not knowing any of your syntax, this is something you could do:
$sp=strpos($word,$row['paragraph']);
echo "...".substr($row['paragraph'],$sp-100,200)."...";
Or if you want 100 characters before, and 100 after:
$sp=strpos($word,$row['paragraph']);
echo "...".substr($row['paragraph'],$sp-100,(200+strlen($word)))."...";
Upvotes: 2