steve m Posted August 30, 2007 Share Posted August 30, 2007 Hello, I am writing this simple MySQL and PHP search and what I want to do is to highlight the search term in the results when they are displayed on the screen. I am able to get it to work. but the seach term will only be highlighted(Make it Bold) only if it matches exactly to what was typed in the search box. Is there a way where the search term doesn't have to exactly match in order for it to be highlighted? Below is the code where I change the the search term into bold to be displayed on the screen. <?php $result_manufact = str_replace($splitstr, "<span style=\"color: red; font-weight: bold;\">$splitstr</span>", $manufact); ?> Quote Link to comment https://forums.phpfreaks.com/topic/67351-solved-highlighting-of-a-search-term-in-search-results/ Share on other sites More sharing options...
effigy Posted August 30, 2007 Share Posted August 30, 2007 You mean a fuzzy search? Try these Google results. Quote Link to comment https://forums.phpfreaks.com/topic/67351-solved-highlighting-of-a-search-term-in-search-results/#findComment-337905 Share on other sites More sharing options...
steve m Posted August 30, 2007 Author Share Posted August 30, 2007 No, I wasn't thinking of a Fuzzy Search. It just a search in one table and it just splits up the phrases (if any) and searches that one table. It seems to find the search term in the database regardless if it is typed in caps or not. The triuble I have is being able to display thr results. I want the search term to be displayed in bold in the results. Example: searched for "time". In the results, I want that term will be in bold, like "times". That works fine as long as the text case is the same. "Shoe will show up if it was all caps but it won't be in bold. I was just wondering if there is a way that the search can show up in bold for example regardless of how the term is typed in? Not sure if I made any sense here . Quote Link to comment https://forums.phpfreaks.com/topic/67351-solved-highlighting-of-a-search-term-in-search-results/#findComment-337934 Share on other sites More sharing options...
effigy Posted August 30, 2007 Share Posted August 30, 2007 Try str_ireplace? Quote Link to comment https://forums.phpfreaks.com/topic/67351-solved-highlighting-of-a-search-term-in-search-results/#findComment-337941 Share on other sites More sharing options...
steve m Posted August 30, 2007 Author Share Posted August 30, 2007 Thanks effigy, but if I read that right str_ireplace ony works for PHP 5. The place here is not running PHP 5. They are running PHP 4?. I think that is exactly what I was looking for, but since they arn't using PHP 5, is there another way I can around this? Quote Link to comment https://forums.phpfreaks.com/topic/67351-solved-highlighting-of-a-search-term-in-search-results/#findComment-337960 Share on other sites More sharing options...
effigy Posted August 30, 2007 Share Posted August 30, 2007 Sure. Use a case-insensitive preg_replace: <pre> <?php $search_term = 'ABC'; $content = 'abcdefghijklmnop'; echo preg_replace('/(' . preg_quote($search_term, '/') . ')/i', '<b>$1</b>', $content); ?> </pre> Quote Link to comment https://forums.phpfreaks.com/topic/67351-solved-highlighting-of-a-search-term-in-search-results/#findComment-337967 Share on other sites More sharing options...
steve m Posted August 30, 2007 Author Share Posted August 30, 2007 Thanks a lot! That worked great. Thanks again effigy. Quote Link to comment https://forums.phpfreaks.com/topic/67351-solved-highlighting-of-a-search-term-in-search-results/#findComment-337983 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.