Jump to content

Trying To Format Form Input For Use In Mysql Fulltext Boolean Search - Preg_Replace Is Failing Me


lb3000

Recommended Posts

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.