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