Page 1 of 1

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

Posted: Tue Sep 18, 2012 9:27 am
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

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

Posted: Tue Sep 18, 2012 12:40 pm
by chris
Sound like a problem in your regex.

If you post some of you code, we could check it out.

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

Posted: Thu Sep 20, 2012 12:50 pm
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];

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

Posted: Fri Sep 21, 2012 7:23 pm
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];

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

Posted: Wed Sep 26, 2012 1:24 pm
by mister_v
Thanks,

preg_quote() solved it for me.

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

Posted: Tue Nov 13, 2012 8:26 pm
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.