OzzC
OzzC

Reputation: 821

Showing text excerpt of the searched word its not working correctly

I'm doing a search system and in this system Im showing the search results but also a bit of text wich begins with searched word.

But Im having one problem and Im not how to solve it.

The problem is: I have in database a word "Constitution", and for example, If I search for "Const" I get my result and my bit of text "Constitution", but if I search for "cons" I get my result but the problem is that Im not having my bit of text with "Const...."

Do you see some way to fix this?

My issue code:

$search = $url[1];
$read = $pdo->prepare("SELECT * FROM pages WHERE title LIKE ? OR content LIKE ? LIMIT ?,?"); 
$read->bindValue(1, "%$search%", PDO::PARAM_STR);
$read->bindValue(2, "%$search%", PDO::PARAM_STR);
$read->bindParam(3, $begin,PDO::PARAM_INT);
$read->bindParam(4, $max,PDO::PARAM_INT);
$read->execute();
$searchPos = stripos($result['content'],$search);
$searchLen = strlen($search);
$result_text = '"'.substr($result['content'], $searchPos, $searchLen + 35).'..."';
echo '<p>'.strip_tags($result_text).'</p>';

Upvotes: 1

Views: 51

Answers (1)

bejs
bejs

Reputation: 156

You are probably having issues with case sensitivity.

Try using stripos instead. Your query is already case-insensitive since you're using LIKE.

$searchPos = strpos($result['content'],$search);

Upvotes: 1

Related Questions