2

I am porting a office managing system from php 5.3 to 5.4 and the errors consists that many functions implement pass-by-reference (the "&" symbol in arguments) and this stops the application runtime. I have solved in the following way:

//This is a function with pass by reference:
function myfunc(&$x, $y, &$z) { ... }

//This is the above function being implemented:
$myClass->myfunc(&$var1, $var2, &$var3);

The php documentation tells me that I have to remove the "&" when I implement the function, so I have to replace that line for:

$myClass->myfunc($var1, $var2, $var3);

because the function definition already have the symbol meaning that argument comes by reference.

But I have so many php files, more than 800 files and I would have to replace one by one, line by line.

So I just need a regex that helps me locate all this "&$" coincidences (avoiding a && that means AND, and any of "&$" coincidences in a function declaration).

I built this regex: [^&]&\$ and it works but need to exclude any coincidence that starts the line in "function" (it would be a function declaration).

AbcAeffchen
  • 14,400
  • 15
  • 47
  • 66

1 Answers1

0
^(\s*function.*)$|(?<!&)&(?=\$[^(]*\))

Try this.This should work for you.Replace by $1.

See demo.

https://regex101.com/r/sH8aR8/35

Your regex [^&]&\$ does not use 0 width assertions.It will replace 3 characters.So it's better to use lookaheads and lookbehnids.

$re = "/^(\\s*function.*)$|(?<!&)&(?=\\$[^(]*\\))/im";
$str = "&\$)\n&&\$\n\$myClass->myfunc(&\$var1, \$var2, &\$var3);\nfunction myfunc(&\$x, \$y, &\$z) { ... }\n\n";
$subst = "$1";

$result = preg_replace($re, $subst, $str);
vks
  • 67,027
  • 10
  • 91
  • 124