dannyb785 Posted March 4, 2012 Share Posted March 4, 2012 I've got a search page that spits out results that match the term that was input. Within the results, I want to add a span with a yellow background behind just the search term. I'll show my code, then say my problem <?php $search = (term in $_GET variable that was searched for); // while loop that goes through all results, $result is the text that the searched term was found in $new_result = str_ireplace($search, "<span style='background:yellow'>$search</span>", $result); echo $new_result; // end while loop ?> my problem is that if somebody searches for the word "bob", then if the term "Bob" was found, when it outputs the $new_result, it shows as "bob" with the yellow background. Searching for "Bob" would give the correct results("Bob" with a yellow background), but basically when the output is given, the case of each letter is displayed as it was typed in the search field. Quote Link to comment https://forums.phpfreaks.com/topic/258229-highlighting-background-behind-search-term-in-results/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 4, 2012 Share Posted March 4, 2012 You would use preg_replace to leave the letter-case as is. See this example - http://www.phpfreaks.com/forums/index.php?topic=350793.msg1655792#msg1655792 Quote Link to comment https://forums.phpfreaks.com/topic/258229-highlighting-background-behind-search-term-in-results/#findComment-1323670 Share on other sites More sharing options...
dannyb785 Posted March 4, 2012 Author Share Posted March 4, 2012 perfect, that fixes it, thanks! but I dunno how to set a topic as solved Quote Link to comment https://forums.phpfreaks.com/topic/258229-highlighting-background-behind-search-term-in-results/#findComment-1323917 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.