lb3000 Posted September 28, 2012 Share Posted September 28, 2012 I am trying to format form text input for use in a mysql fulltext boolean mode search. If someone wants to match "all" words in the input box, I need to put a "+" sign in front of every word. If they choose to match "any" words, we can ignore this step. If they enter a phrase -- indicated by wrapping the phrase in quotes (eg., "search me") I need to include that quoted string as is. Easier to see with an example: Input: one two "test test" Assuming someone has asked to get a result matching "all" words/phrases, I should format this input like so: +one +two "test test". Here's my code to do this formatting. It works great up until, of course, the last step. I end up with this output: +one +two +"test +test". The + inside of the quoted phrase -- "test +test" -- is the problem. It seems preg_replace() is just skipping over doing the replacing I need. Anyone see anything weird here? $keywords = 'one two "test test"'; // Checking that the $keywords string doesn't start with a quotation mark. If not - this is a word, add a + if (substr($keywords, 0,1) != '"') { $keywords = '+' . $keywords; } // Rewriting $keywords so that every space in the string now includes a + -- will use this in a sec to replace the quoted phrase with "+" with the original quoted phrase - no + $prep_string = str_replace(' ', ' +', $keywords); // now $keywords = +one +two +"test +test" // Finding any quoted phrases in the original $keywords string here $pattern = "/\"[^\"]*\"/"; preg_match_all($pattern, $keywords, $matches); // the match = "test test" if (!empty($matches[0])) // $matches[0] is the returned array that holds any matches found in the original $keywords string { $prepped = array(); foreach ($matches[0] AS $value) { // Looping through and preparing each value (here the value is just "test test") to include a + // This is so that I have a way to match values in the $prep_string string $prepped[] = '/' . str_replace(' ', ' +', $value) . '/'; // this outputs '/"test +test"/' } $new_string = preg_replace($prepped, $matches[0], $prep_string); $new_string = str_replace('+"', ' "', $new_string); // The rest here is just to dump all of this to screen, you'll see that preg_replace didn't replace "test +test" found in $prepped with "test test" found in $matches[0] 'original matches: ' . var_dump($matches[0]); echo '<br />'; 'prepped matches: ' . var_dump($prepped); echo '<br />'; echo 'preg_replace output: ' . $new_string; // this is } If you var_dump matches[0] and $prepped, and then echo $new_string, here's what you see: original matches: array(1) { [0]=> string(11) ""test test"" } prepped matches: array(1) { [0]=> string(14) "/"test +test"/" } preg_replace output: +one +two +"test +test" ====== this should be +one +two "test test" Link to comment https://forums.phpfreaks.com/topic/268891-trying-to-format-form-input-for-use-in-mysql-fulltext-boolean-search-preg_replace-is-failing-me/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.