Replace only specific group in file form preg_replace

Viewed 4643

I have txt file with content:

fggfhfghfghf

$config['website'] = 'Olpa';


asdasdasdasdasdas

And PHP script for replacing by preg_replace in file:

write_file('tekst.txt', preg_replace('/\$config\[\'website\'] = \'(.*)\';/', 'aaaaaa', file_get_contents('tekst.txt')));

But it doesn't work exactly what I want it to work.

Because this script replace whole match, and after change it looks like this:

fggfhfghfghf

aaaaaa


asdasdasdasdasdas

And that's bad.

All I want is to not change whole match $config['website'] = 'Olpa'; But to just change this Olpa

enter image description here

As you can see it belongs not to Group 2. of match information.

And all I want is to just change this Group 2. one specific thing.

to finally after script it will look like:

fggfhfghfghf

$config['website'] = 'aaaaaa';


asdasdasdasdasdas
2 Answers
Related