Jump to content

Help with Search And Replace Function


refiking

Recommended Posts

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

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?

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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.