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. Link to comment https://forums.phpfreaks.com/topic/66403-replacing-text/ 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. Link to comment https://forums.phpfreaks.com/topic/66403-replacing-text/#findComment-332346 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..? Link to comment https://forums.phpfreaks.com/topic/66403-replacing-text/#findComment-332349 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; } } Link to comment https://forums.phpfreaks.com/topic/66403-replacing-text/#findComment-332360 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 Link to comment https://forums.phpfreaks.com/topic/66403-replacing-text/#findComment-332368 Share on other sites More sharing options...
effigy Posted August 24, 2007 Share Posted August 24, 2007 This, perhaps? Link to comment https://forums.phpfreaks.com/topic/66403-replacing-text/#findComment-333064 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.