Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php 5.3.6 breaks my regex pattern

Tags:

regex

php

Weird things happened when a website I am working on got moved to another server with newer version of PHP. The regex that was working normally on 3 other different servers refuses to work, returns 0 all the time.
This is the original regex:

/\A(?P<text>[[:alpha:]]{4})[[:space:]](?P<password>[\p{L}\p{N}]{3,})[[:space:]](?P<digit1>[[:digit:]]{2,4})[[:space:]](?P<digit2>[[:digit:]]{1,2})\z/u

password had to be unicode, before it was just [[:alnum:]] but there were some problems with clients.
And these are the ones I tried to no avail:

/\A(?<text>[[:alpha:]]{4}) (?<password>[[:alnum:]]{3,}) (?<digit1>[[:digit:]]{2,4}) (?<digit2>[[:digit:]]{1,2})\z/u
/\A(?<text>[\p{L}]{4}) (?<password>[\p{L}\p{N}]{3,}) (?<digit1>\d{2,4}) (?<digit2>\d{1,2})\z/u

Example text: ABCD 1a2b3c 100 50

All of these were tested on http://www.spaweditor.com/scripts/regex/index.php and worked perfectly.

Can anyone tell me what's wrong with it? I know it might not be the prettiest regex you've seen but it works (or at least worked until now) just like expected. Right now I'm blaming the PHP (lol).

Edit: tested the last pattern on these sites:
* http://regex.larsolavtorvik.com/
* http://www.spaweditor.com/scripts/regex/index.php
* http://www.pagecolumn.com/tool/pregtest.htm
* http://lumadis.be/regex/test_regex.php
and on some others where it allowed to type my regex fully myself, with slashes and u flag. In all of these websites it worked, but on some others I checked it didn't, same as my server.

PCRE is unicode-enabled...

Edit: seems that sysadmins changed something after all... didn't work until 10 minutes earlier :/ Somebody close this question, all of the regexes now work.

like image 265
jurchiks Avatar asked May 25 '26 08:05

jurchiks


1 Answers

You should check your PCRE extension version (phpinfo() -> PCRE Library Version). Some servers have < 8, and version >= 8 only supports unicode.

Here is a good example on how to fix the problem on CentOS with PHP 5.2, you can check it with PHP 5.3 also.

like image 149
donis Avatar answered May 27 '26 20:05

donis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!