sleepless_in_seattle
sleepless_in_seattle

Reputation: 2224

preg_match_all behaving weird on different servers

I tried reading the other questions first but didn't find anything useful.

I have a development server with PHP 5.3.28 and PHP 5.4.7 on my own PC.

$string = '<if test="sub_function(parameter_1, parameter_2)">Match this</if>';
preg_match_all("/<if test=['\"]([!\w|&(),-\s]+)['\"]>(.*?)<\/if>/s", $string, $matches, PREG_SET_ORDER);

The code above works fine on my xampp environment but doesn't work on the x86_64-redhat-linux-gnu server. The $matches is false on the linux environment and looks like this on my xampp:

Array
(
    [0] => Array
        (
            [0] => Match this
            [1] => sub_function(parameter_1, parameter_2)
            [2] => Match this
        )
)

What could be causing this behaviour? This also works with my other PC with like PHP 5.2 - 5.3.

EDIT: The regexp is deliberately complicated, it has more uses than the one I'm showing you. What I want to know is the things that affect preg_match behavior on the server.

Upvotes: 1

Views: 549

Answers (1)

georg
georg

Reputation: 215009

I think the problem is here

[!\w|&(),-\s]

This is not a valid range. Some PCRE versions are more permissive and allow stuff like that, but (at least, on OSX), php 5.5 and 5.6 refuse to run this saying that

Warning: preg_match_all(): Compilation failed: invalid range in character class at offset 25...

You can easily fix that by escaping - or moving it to the end:

[!\w|&(),\-\s]

Also, it's always helpful to set error_reporting to E_ALL.

Upvotes: 3

Related Questions