Page 1 of 1

Trying to make a search script..

Posted: Thu Jun 29, 2006 8:31 pm
by Benjamin
My regex is a little wacked. It's isn't staying inside quote pairs. I'm going to regex single words later. I just wanted to get all the phrases out first. Should I split up the string first?

Code: Select all

// create an array of excluded phrases
    preg_match_all('#\s*\-".*?"\s*#', $_POST['SearchQuery'], $ExcludedPhrases);

    echo '<p><b>Excluded Phrases:</b></p>';
    echo '<pre>';
    print_r($ExcludedPhrases[0]);
    echo '</pre>';

    // create an array of included phrases
    preg_match_all('#\s*[^-]\+*".*?"\s*#', $_POST['SearchQuery'], $IncludedPhrases);

    echo '<p><b>Included Phrases:</b></p>';
    echo '<pre>';
    print_r($IncludedPhrases[0]);
    echo '</pre>';
Outputs...


RECEIVED: hi -"don't include me" +findthis -don'tfindthis +"find this phrase"

Excluded Phrases:

Array
(
[0] => -"don't include me"
)

Included Phrases:

Array
(
[0] => e" +findthis -don'tfindthis +"
)

Posted: Fri Jun 30, 2006 3:00 pm
by Benjamin
I guess my question wasn't written very well.

When I send the text hi -"don't include me" +findthis -don'tfindthis +"find this phrase"

The included array is populated like this...

Included Phrases:

Array
(
[0] => e" +findthis -don'tfindthis +"
)

When it should be

Array
(
[0] => +"find this phrase"
)

Can I fix this with my RegEx or should I do some sort of preprocessing on the text?

Posted: Fri Jun 30, 2006 10:37 pm
by Benjamin
Ok, I changed it to..

Code: Select all

preg_match_all('#\+"(.*)" #', $_POST['SearchQuery'], $IncludedPhrases);
Now from this string...
hi -"don't include me" +findthis -don'tfindthis +"find this phrase" more words +"another phrase"
It matches..

hi -"don't include me" +findthis -don'tfindthis +"find this phrase" more words +"another phrase"

It does this because I put a space at the end of the regex, otherwise it matches...

hi -"don't include me" +findthis -don'tfindthis +"find this phrase" more words +"another phrase"

I need it to match like this...

hi -"don't include me" +findthis -don'tfindthis +"find this phrase" more words +"another phrase"

Anyone?

Posted: Mon Jul 03, 2006 2:50 pm
by Skara
try changing
#\+"(.*)"#
to
#\+"(.*?)"#

and actually a + might be better...
#\+"(.+?)"#