Trying to make a search script..

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Trying to make a search script..

Post 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 +"
)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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?
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

try changing
#\+"(.*)"#
to
#\+"(.*?)"#

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