refiking Posted May 30, 2010 Share Posted May 30, 2010 I am trying to replace the requested term with the a span tag everytime that term is found, whether it is a portion of a word or the whole word. When I tried str_replace, it didn't work and when I tried preg_replace, it didn't work. Here are the 2 functions within the code. What do I need to change? $mynotes = str_replace($_POST['terms'], '<span id="hilite">'.$_POST['terms'].'</span>', $mynotes); $mynotes = preg_replace($_POST['terms'], '<span id="hilite">'.$_POST['terms'].'</span>', $mynotes); Link to comment https://forums.phpfreaks.com/topic/203357-help-with-search-and-replace-function/ Share on other sites More sharing options...
refiking Posted May 30, 2010 Author Share Posted May 30, 2010 Update: It appears that the str_replace worked, but only if the case of the letters matches. For example, when $_POST['terms'] = 'no'; it doesn't add the span to the word "Note". But, when $_POST['terms'] = 'No'; it adds the span to the No within Note. Is there some kind of way I can make it ignore the case when searching? Link to comment https://forums.phpfreaks.com/topic/203357-help-with-search-and-replace-function/#findComment-1065365 Share on other sites More sharing options...
kenrbnsn Posted May 30, 2010 Share Posted May 30, 2010 Use str_ireplace Link to comment https://forums.phpfreaks.com/topic/203357-help-with-search-and-replace-function/#findComment-1065366 Share on other sites More sharing options...
refiking Posted May 30, 2010 Author Share Posted May 30, 2010 the only problem with str_ireplace is the fact that it changes the case of the output as well. So, when I processed the form, it returned note instead of Note. Anyway to have it keep it's original case? Link to comment https://forums.phpfreaks.com/topic/203357-help-with-search-and-replace-function/#findComment-1065367 Share on other sites More sharing options...
refiking Posted May 30, 2010 Author Share Posted May 30, 2010 OK. So, what I attempted didn't work, either. it worked if the first letter was uppercase because that's the last str_replace function. Any other ideas guy? $mynotes = str_replace($_POST['terms'], '<span class="hilite">'.$_POST['terms'].'</span>', $mynote); $mynotes = str_replace(ucwords($_POST['terms']), '<span class="hilite">'.ucwords($_POST['terms']).'</span>', $mynote); Link to comment https://forums.phpfreaks.com/topic/203357-help-with-search-and-replace-function/#findComment-1065368 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.