This is a replacement gotcha. From the PHP manual documentation on preg_replace()
:
When working with a replacement pattern where a backreference is immediately followed by another number (i.e.: placing a literal number immediately after a matched pattern), you cannot use the familiar
\\1
notation for your backreference.\\11
, for example, would confusepreg_replace()
since it does not know whether you want the\\1
backreference followed by a literal1
, or the\\11
backreference followed by nothing. In this case the solution is to use\${1}1
. This creates an isolated$1
backreference, leaving the1
as a literal.
This should do what you want:
preg_replace( '/Version:([ ]*)[0-9\.]+/', 'Version:${1}'.$new_version, ...);