chewbears Posted March 15, 2011 Share Posted March 15, 2011 So I have a search function (part posted below). I have it set up that a user passes asterisk in the way that quotes would work in a normal search function. Due to the scope of the search function a pagination function can not handle quotes because it breaks the html. So what I need to do is transform the quotes a user would type into the search box into asterisks so the function will handle everything. I am assuming this is done with preg_match, but I am not familiar with preg_match at all other then what is written in manuals. <?php $arr['searchterms'] = trim($arr['searchterms']); if (strstr($arr['searchterms'],'*+')) { $terms = explode('*+*',substr($arr['searchterms'],1,-1)); $case = "AND"; } else if (strstr($arr['searchterms'],'*')) { $terms = explode('* *',substr($arr['searchterms'],1,-1)); $case = " OR"; } else { $terms = explode(" ",$arr['searchterms']); $case = " OR"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/230696-preg_match-change-to-from-searchbox/ Share on other sites More sharing options...
Adam Posted March 15, 2011 Share Posted March 15, 2011 Due to the scope of the search function a pagination function can not handle quotes because it breaks the html. Run the string through htmlentities() before you output it. For future reference though, you wouldn't need to use regular expressions to replace a character within a string. You can just use str_replace(). Quote Link to comment https://forums.phpfreaks.com/topic/230696-preg_match-change-to-from-searchbox/#findComment-1187757 Share on other sites More sharing options...
PFMaBiSmAd Posted March 15, 2011 Share Posted March 15, 2011 Edit: LOL, duplicate information from above ^^^ pagination function can not handle quotes because it breaks the html ^^^ Not if you use htmlentities(), with the second parameter set to ENT_QUOTES, on DATA being output to the browser that could contain HTML special characters and/or use urlencode() if this is going into a URL. What are you actually doing at the point that the HTML gets broken by a ' ? Quote Link to comment https://forums.phpfreaks.com/topic/230696-preg_match-change-to-from-searchbox/#findComment-1187758 Share on other sites More sharing options...
chewbears Posted March 15, 2011 Author Share Posted March 15, 2011 The above does not break only when I switch the * to a double qoute ". I can not str_replace because searchterms is not a global variable so when the pagination goes to get it it uses a get and does not look at what has been done to the searchterms in the search function. Kinda not smart coding but it works for the time being. Htmlenties may work but I am not sure how to use it within the get of the pagination builder. Working on that now. Quote Link to comment https://forums.phpfreaks.com/topic/230696-preg_match-change-to-from-searchbox/#findComment-1187767 Share on other sites More sharing options...
chewbears Posted March 15, 2011 Author Share Posted March 15, 2011 htmlentities doesn't work as it breaks the search as it passes the qoutes properly, which I need to drop in order for the string to exist in the database. Because I do not have "Big Bend". I have Big Bend in a field. So the Query before passed %'big bend'% with the htmlentities it passes %"big bend"% Quote Link to comment https://forums.phpfreaks.com/topic/230696-preg_match-change-to-from-searchbox/#findComment-1187779 Share on other sites More sharing options...
chewbears Posted March 15, 2011 Author Share Posted March 15, 2011 fixed it. Just changed all instances of the variable search terms with str_replace and all is well. Thanks for the help, sorry for my near sighted problem solving ability Quote Link to comment https://forums.phpfreaks.com/topic/230696-preg_match-change-to-from-searchbox/#findComment-1187792 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.