Error with Php and Preg_match

Asked

Viewed 243 times

1

I have the following line in a php file

if(preg_match("!\oa!", $id)){

until a couple of months ago this worked normally but began to give this error

Warning: preg_match() [Function.preg-match]: Compilation failed: Missing Opening Brace after o at offset

anyone can tell me what’s going on ? I used some regular expression validators online and returned no errors.

  • tried to change to single quote?

  • but it hasn’t solved

  • try to change ! for ~

  • What happened in those 2 months ? Upgraded php to another version?

  • What is the purpose of this regex? what it should do?

1 answer

1


Is probably a bug in the library version PCRE that you use, upgrade to the latest version.

As can be seen in changelog, item 32:

  1. Error messages for syntax errors following \g and \k Were Giving inaccurate offsets in the Pattern.

The error can be seen in this example which uses version 8.35:

echo PCRE_VERSION; // 8.35 2014-04-04

var_dump(preg_match('/\k/', 'foo')); 
// \k is not followed by a braced, angle-bracketed, or quoted name at offset 2

var_dump(preg_match('/\g/', 'foo')); 
//  a numbered reference must not be zero at offset 1

An alternative is to escape \k, \g and in your case \o with a double reverse bar \\:

$id = "\oa";

if(preg_match("!\\\oa!", $id)) {
    echo "Match!";
} else {
    echo "No match.";
}

See DEMO

  • solved with double scape ^^

  • only I did not understand why need three scape symbols

  • @Jasarorion It is to specify \ literally. Here has some more information.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.