在表达式后写入文件


Write in file after an expression

我有这个文件和regex:

$pattern = '/.*services {1,}:/';
$services = file_get_contents($services_file);
preg_match($pattern, $services, $matches);
dump($matches)

我能够在文件中找到我的表达式,现在我想在它后面写一些东西。

使用preg_replace()$0与模式完全匹配,并在其后面添加$replacement文本:

$result = preg_replace($pattern, "$0$replacement", $services);
file_put_contents($services_file, $result);