Searching For Strings In Text

[font=times new roman]I am trying to make a little script that allows a user to search for blocks of text within strings. The user enters data into form fields and he or she can enter text into another form field (needle) to search the data fields (haystack). When the search string matches something in the data fields the associated data fields are highlighted in a yellow background color.

Right now the search string is acting funny. W[/font]hen I enter a search string I get no highlighting unless if the first character(s) of the search string are the same as the first character(s) for the items. For instance, If I search for the text “at” in the word “bat” I will not get any yellow highlighting. But I would get highlighting for “bat” if I [size=3]search for “ba.”[/size] How would I change the code so that any data field is highlighted if the search string exists anywhere in the text for the data field?

Also, I figured out how to stop the form fields from being yellow if they and the search field are empty/NULL, but I did this part in another file (as an IF statement) and can’t seem to get it to work in [font=times new roman]the other file. How would I make it do the highlighting if and only if there is a search string in the search field (i. e. only highlighting when the search field is not NULL/empty).

The code from my 2 files is here…: http://pastie.org/1095526 , http://pastie.org/1095528
[/font]
Thanks very much to anyone who can help me.

It would help if you post piece of code you need help with here, surrounding by [ php ] tag (use the “php” button in the toolbar). Probably no need to post entire script.

Sounds like you need the str_replace function: http://php.net/manual/en/function.str-replace.php

You can use it and replace all occurrences of the value with the same text formatted in the way you want using a span element.

Example: str_replace($textToFind, “”.$textToFind."", $textToSearch)

Sponsor our Newsletter | Privacy Policy | Terms of Service