receiver Posted December 25, 2006 Share Posted December 25, 2006 Please Help me with Text highlighting in search results:The function should bestr_replace( $term, "<b>".$term."</b>", $search_results );I need to enter the above code into the code sourcethe search type is like(search engine gets db results, the type of source)[code]$searchquery="select * from site where (name like'%$term%' or url like'%$term%' or `desc` like'%$term%') and credits > 0 and state = 'Enabled' order by credits DESC limit $start,$resultperpage";//echo $searchquery; $result=mysql_query($searchquery); if(mysql_num_rows($result)!=0){ for($x=0;$x<mysql_numrows($result);$x++){echo(" ".mysql_result($result,$x,'name')." <br> ".mysql_result($result,$x,'desc')." ");[/code]I am not sure what to enter into the $search_results to work with the search engine script, maybe $searchquery but i tried.($term is searched keyword)can you help meThank You,Thomas Link to comment https://forums.phpfreaks.com/topic/31832-solved-help-with-text-highlighting-in-search-results/ Share on other sites More sharing options...
receiver Posted December 26, 2006 Author Share Posted December 26, 2006 I make it simpler.the code has replace function(php)(should be right) [code]tr_replace( $search_term, "<span>".$search_term."</span>", $search_results );[/code]all $search_term should be replaced with <span>".$search_term."</span>I entered my search engine script above in the first post just to you, that you can see what type of script the search engine is, not all the script just a part from the script.I need the replace function into the script, but I need help with it, to put the replace function into the search engine script so that it will work also.I am not 100% sure what i need to enter to "$search_term" that the function uses the search engine script right.If you have any comments or more questions please post it, thank you for helping me.Thomas Link to comment https://forums.phpfreaks.com/topic/31832-solved-help-with-text-highlighting-in-search-results/#findComment-147935 Share on other sites More sharing options...
bljepp69 Posted December 26, 2006 Share Posted December 26, 2006 Here's a function I've used to make the search term bold in the results:[code]<?php function formatSearch($text,$term) { return preg_replace("/{$term}/i",'<b>${0}</b>',$text); //note the ${0} in the replacement term keeps the original string that matched the search term }?>[code]So, in your code, you might use it something like:[code]<?php$searchquery="select * from site where (name like'%$term%' or url like'%$term%' or `desc` like'%$term%') and credits > 0 and state = 'Enabled' order by credits DESC limit $start,$resultperpage";//echo $searchquery; $result=mysql_query($searchquery); if(mysql_num_rows($result)!=0){ while ($row = mysql_fetch_assoc($result)) { echo " ".formatSearch($row['name'],$term)." <br /> ".formatSearch($row['desc'],$term)." "; } }?>[/code][/code][/code] Link to comment https://forums.phpfreaks.com/topic/31832-solved-help-with-text-highlighting-in-search-results/#findComment-147971 Share on other sites More sharing options...
receiver Posted December 26, 2006 Author Share Posted December 26, 2006 Thank you for your help bljepp69. It helped me a lot Link to comment https://forums.phpfreaks.com/topic/31832-solved-help-with-text-highlighting-in-search-results/#findComment-148048 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.