How to add a file after a line? Php

Ok lets say that I have a file called File.txt and it contains

Hello
World
!!!!!

Ok, now I have this so that they can send something added to the string. But it should be right below the line "Hello." How should I do it?

+3
source share
1 answer
$mystring="string to append";
$file="file";
$data = file($file);
foreach ($data as $k=>$v){
    if ( strpos($v,"Hello") !==FALSE){
        $data[$k]=$data[$k] . "$mystring\n";
    }
}
file_put_contents($file,$data);
+4
source

Source: https://habr.com/ru/post/1733462/


All Articles