Rizwan Khan
Rizwan Khan

Reputation: 401

How do i retrieve matched string of a paragraph using php

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

Answers (2)

exussum
exussum

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

Tim Withers
Tim Withers

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

Related Questions