tags:

views:

38

answers:

2

hi

i want to know how to search and display string from group of string like google display the word which we search...

the following example from google

search word is google books

result

Search and preview millions of books from libraries and publishers worldwide using Google Book Search. Discover a new favorite or unearth an old classic.

i would like to do this type of one...

thanks and advance

+2  A: 
$searchString = "Google Book";

$originalString = "Search and preview millions of books from libraries and publishers worldwide using Google Book Search. Discover a new favorite or unearth an old classic.";

$outputString = preg_replace("/($searchString)/i",'<strong>${1}</strong>',$originalString);

This should do a case-insensitive search and replace of your search string so that it becomes wrapped by <strong> tags.

Note, however, it will not match if you provide a search string of "Google Books" (note the 's'). This is very basic, there is no stemming to account for variations of words / plurals etc nor does it check for word boundaries (ie you could search for 'oogl' and it will match Google). Depending on your requirements, this might be sufficient though.

Mailslut
yes, it s correct one, but i need to display only 150 characters. so then how to change this coding.. explain me....
zahir hussain
after that use like this,$outputstring = substr($outputString,0,150);
Karthik
suppose my total string is 5000 characters, i searched string is where i dont know the position. in this time hw can i get correct position... thats i asked can i get particular 150 characher from string and also contain my searched word...
zahir hussain
Sorry, I dont understand what you're asking. Can you rephrase?
Mailslut
The string have more characters...the searched word in the starting or ending or mid position...we dont know the location...so how can i get the word correctly...sorry i lack of communication...so thats y i cant explain correctlyy...
zahir hussain
now u can clear to get?
zahir hussain
+1  A: 

You can also go with strtr, it can be faster on large chunks of text.

$text = strtr($text, array("search term" => "<b>search term</b>"));

Be careful to use the array as parameter, as this function behavior is slightly different using string parameters

Benoit