PHP Warning: preg_match(): Compilation failed: missing ) at

All your question about php
Post Reply
mister_v
Posts: 188
Joined: Thu Mar 04, 2010 9:19 pm

PHP Warning: preg_match(): Compilation failed: missing ) at

Post by mister_v »

Hi,

I get the following warning message:

Code: Select all

PHP Warning:  preg_match(): Compilation failed: missing ) at offset 28 in file on line 35
Can anyone tell me what it means?
And what I can do to solve it?

Thanks
chris
Site Admin
Posts: 194
Joined: Mon Jul 21, 2008 9:52 am

Re: PHP Warning: preg_match(): Compilation failed: missing

Post by chris »

Sound like a problem in your regex.

If you post some of you code, we could check it out.
mister_v
Posts: 188
Joined: Thu Mar 04, 2010 9:19 pm

Re: PHP Warning: preg_match(): Compilation failed: missing

Post by mister_v »

This is the code:

Code:

Code: Select all

$pattern="/\b".$c_line[1]."\b/i";
if(preg_match($pattern,$text)) $countries[]=$c_line[0];
chris
Site Admin
Posts: 194
Joined: Mon Jul 21, 2008 9:52 am

Re: PHP Warning: preg_match(): Compilation failed: missing

Post by chris »

There is probably a character in $c_line[1] that breaks your regular expression ($pattern).

You can use preg_quote() to escape special characters.

Your code should look something like this:

Code: Select all

$pattern="/\b".preg_quote($c_line[1])."\b/i";
if(preg_match($pattern,$text)) $countries[]=$c_line[0];
mister_v
Posts: 188
Joined: Thu Mar 04, 2010 9:19 pm

Re: PHP Warning: preg_match(): Compilation failed: missing

Post by mister_v »

Thanks,

preg_quote() solved it for me.
chris
Site Admin
Posts: 194
Joined: Mon Jul 21, 2008 9:52 am

Re: PHP Warning: preg_match(): Compilation failed: missing

Post by chris »

If you still have problems even with

Code: Select all

$pattern="/\b".preg_quote($c_line[1])."\b/i";
Try
$pattern="/\b".preg_quote($c_line[1],'/')."\b/i";
It should escape any 'bad' character.
Post Reply