Question

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 );

Was it helpful?

Solution

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.

OTHER TIPS

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top