2

I have a text field where the user will enter comma separated keywords or key phrases, and the server will then use these values to check multiple bodies of text for matches.

So basically what I need is to match an exact phrase, case insensitive, with possible spaces in a body of text.

I can match keywords easily, by generating the following regex:

Example keywords: peanut, butter, jelly

Regex generated: /peanut|butter|jelly/i

However having spaces does not work. Even if I replace the spaces in the given values with \s

Example: peanut butter, jelly sandwich, delicious

Regex: /peanut\sbutter|jelly\ssandwich|delicious/i

What would be a correct regex to match the phrases exactly ? Case insensitive and using PHP's preg_match ?

Thanks.

EDIT

This is what I am doing:

$keywordsArray = array_map( 'trim', explode( ',', $keywords ) );
$keywordsArrayEscaped = array_map( 'preg_quote', $keywordsArray );
$keywordsRegex = '/' . implode( '|', $keywordsArrayEscaped ) . '/i';

The above generates the expressions as described above ( Without the replacement of spaces to \s, since it didn't work. )

Following that I simple do preg_match( $keywordsRegex, $text );

6
  • 1
    I don't see thee issue. Spaces work for in preg_match for me. Provide some code. Commented Jul 23, 2013 at 16:26
  • where are the target text you're trying to check come from? are they encoded like  ? Commented Jul 23, 2013 at 16:28
  • Would $keywords = explode(", ", $in); not be easier? Commented Jul 23, 2013 at 16:28
  • show us your code. impossible to help unless you do, because we don't know what's actually going on in your specific situation. preg_match can handle both spaces and \s just fine, so that's not the problem. Commented Jul 23, 2013 at 16:30
  • Yea, but I am walking the array and trimming the values. So it's pretty much the same thing. Commented Jul 23, 2013 at 16:30

2 Answers 2

3

I don't see why it wouldn't work with spaces or \s. It should. But to answer the question you asked in general terms, the way to match exact phrases in a regex is to surround them with \Q and \E:

/\Q<phrase 1>\E|\Q<phrase 2>\E|\Q<phrase 3>\E/

That's normally used for text that contains escapes or regex metacharacters. You really shouldn't need that for spaces.

Sign up to request clarification or add additional context in comments.

Comments

0

The only issue I could really find with your code is that you aren't filling in the third field for the results;

preg_match($keywordsRegex, $text, $results);

var_dump($results);

1 Comment

Because I'm only concerned with whether or not it matched.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.