adam84 Posted August 23, 2007 Share Posted August 23, 2007 I have a textfield where the user enters a keyword about whatever they are looking for. I send the data to my php file to be processed and what I want to do is highlight the keyword that the user entered in, when I display the all the results that have matched the keyword. What I did was use the str_replace() function, but what I have noticed is the function is case sensitive. So if the user enters 'beer'. Only the word 'beer' will get highlighted, but 'Beer', 'beeR' or 'BEER'. What would I need to do, to make it work on every instance of the word. Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 23, 2007 Share Posted August 23, 2007 You could read the manual: http://us2.php.net/str_replace Read the Notes: Notes Note: This function is binary-safe. Note: This function is case-sensitive. Use **GO FIND OUT** for case-insensitive replace. At the bottom of most function's entries is a list of related functions. Quote Link to comment Share on other sites More sharing options...
adam84 Posted August 23, 2007 Author Share Posted August 23, 2007 awesome, the only thing is that i have version 4.44 and this is version 5. Any other way..? Quote Link to comment Share on other sites More sharing options...
nathanmaxsonadil Posted August 23, 2007 Share Posted August 23, 2007 Dreamhost hasn't upgraded to PHP 5.0 -- I came up with my own alternative, trying to make it run as much like str_ireplace() as possible. Adjust the switch-a-roo token if you think it may conflict with your needle in teh haystack. ## HOMEBREW str_ireplace() FOR PRE-PHP 5.0 if (!function_exists('str_ireplace') { function str_ireplace($search,$replace,$subject) { $token = '^[[term^]'; $haystack = strtolower($subject); $needle = strtolower($search); while (($pos=strpos($haystack,$needle))!==FALSE) { $c++; $subject = substr_replace($subject,$token,$pos,strlen($search)); $haystack = substr_replace($haystack,$token,$pos,strlen($search)); } while (($pos=strpos($subject,$token))!==FALSE) { $subject = substr_replace($subject,$replace,$pos,strlen($token)); } return $subject; } } Quote Link to comment Share on other sites More sharing options...
lemmin Posted August 23, 2007 Share Posted August 23, 2007 ereg_ireplace works in PHP 4. http://us.php.net/manual/en/function.eregi-replace.php Quote Link to comment Share on other sites More sharing options...
effigy Posted August 24, 2007 Share Posted August 24, 2007 This, perhaps? Quote Link to comment 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.