I'm trying to replace a version string but it always eats up my fists letter of the version string until I put a space in between: Version:$1 $new_version
But I do not want to do that and would like to understand what happens here.
File contents:
Version: 4.4.4
Relevant part of my code:
$new_version = 6.6.6
$new_main_file_content = preg_replace(
'/Version:([ ]*)[0-9\.]+/',
"Version:$1$new_version",
file_get_contents($main_file)
);
Without the space the output is Version:.6.6
and with it, the output is right but just one space too much. I have tried using the curly brackets as well, but... no luck.
This is a replacement gotcha. From the PHP manual documentation on
preg_replace()
:This should do what you want:
Demo