aotearoa Posted August 16, 2010 Share Posted August 16, 2010 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. When 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 search for "ba." 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 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 Thanks very much to anyone who can help me. Quote Link to comment https://forums.phpfreaks.com/topic/210863-searching-for-strings-in-text/ Share on other sites More sharing options...
SchweppesAle Posted August 17, 2010 Share Posted August 17, 2010 Something like this? $search = $_POST['search']; if(!is_null($search)) { $replacement = '<font style="background-color: yellow;">'.$search.'</font>'; $string = str_replace($search, $replacement, $string); } Quote Link to comment https://forums.phpfreaks.com/topic/210863-searching-for-strings-in-text/#findComment-1100456 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.