Jump to content

showing few words before and after the search keyword


asmith

Recommended Posts

hey guys

 

I'm writing a search script. 

I want to highlight the keyword found in the content. I will do it with str_replace.  But i also want to show a few words before it and few words after it.  like 

 

... text words bla bla keyword bla bla ...

 

I'm out of ideas how to echo few words before and after the keyword otherwise some contents are so long that will fill all the page.

Can someone please give me some ideas?

Thanks for the reply.

 

the stristr or strstr is a good thing. But It do not get the characters before the parametr given IN EARLIER PHP VERSIONS. I'm running php 4.3.3 .

 

hmmm Just gotta find out how to get the words BEFORE a given word... (in my php version ;) )

 

EDIT :  I found it :D   

 

<?php
$haystack = 'php-homepage-20071125.png';
$needle = '-';
$result = substr($haystack, 0, strpos($haystack, $needle)); // $result = php
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.