arrays - PHP traverse txt, once string is found, insert text -
i have file, test.txt, , traverse it. once hind "here" string, i'd place 'test' in following line.
            $lines = array();             $match = 'here';             foreach(file('test.txt') $line)             {             if((string)$match == (string)$line)             {                      array_push($lines, 'this');;             }             array_push($lines, $line); unfortunately, if statement never resolves true, though there 'here' in txt file.
thanks
credits goes vedran Ĺ ego comment.
the problem $line contains new line, remove we'll using trim()
edit: code may have several problems stated below in comments jon , seems misunderstood wanted (instead of "adding" replaced matched string)
foreach(file('test.txt') $line){    if($match === trim($line)){       array_push($lines, 'this');    }    array_push($lines, trim($line)); } 
Comments
Post a Comment