php5 not working as it used to in php4
Neil Walker

Hello,
In php4 this code works fine and returns the correct data:

function resolve_consts($contents)
{
  return preg_replace_callback("/(@@CONST_\w+)*/","constsplitterfunction",&$contents);
}

But when I use php5 it returns nothing. However, if I change the * to a +, as in:

function resolve_consts($contents)
{
  return preg_replace_callback("/(@@CONST_\w+)+/","constsplitterfunction",&$contents);
}

It works (well, as far as + goes) in php5.

Anyone any idea what's going wrong?

Matthew Leverton

Pass by reference only works in the function declaration in PHP 5, and for good reason. So you'll need to adjust accordingly.

manual said:

Note that there's no reference sign on function call - only on function definition. Function definition alone is enough to correctly pass the argument by reference. In recent versions of PHP you will get a warning saying that "Call-time pass-by-reference" is deprecated when you use a & in foo(&$a);.

So there might be a config option to enable it, but I'd just change the code.

Edit:

Quote:

However, if I change the * to a +, as in:

Oh, maybe my point isn't relevant then. (Although it's still worth mentioning.) I have to run, but I'll look at it more closely later if you still need help.

Jakub Wasilewski

By "it returns nothing", what do you mean exactly? Does preg_replace_callback return NULL, or do you mean something else?

Anyway, preg_replace_callback can fail due to exhausting the recursion or backtrack limit for regular expressions (pcre.* ini values), and I've seen it just return NULL on those occasions. These limits are new in PHP 5.something, so perhaps they are the culprit if you're doing these replacements on large datasets.

Matthew Leverton

Assuming you are using PHP 5.2 (which introduced the limits), try calling preg_last_error().

Neil Walker

thanks.

the pass by reference got me before so I quickly enabled the Call-time pass-by-reference flag in the php.ini to get by (my live server is still 4, but my dev machine is 5).

I'll check the return value to see what's returning, but the strings I'm passing in on my quick test only ever had one instance of @@CONST_, and I'll try preg_last_error() as well.

But I can't actually remember why I put in * and not +, I'm sure the code stopped returning the correct data. In the case of my code above, there shouldn't be a difference between + and * should there?

Matthew Leverton

The expression /(@@CONST_\w+)*/ "matches" anything you send at it because you are searching for zero or more instances of @@CONST_\w+.

Thread #593073. Printed from Allegro.cc