Platypus wrote:
> i need to write php script thats gonna change some TXT file.. in text file i
> have this:
>
> something=
> blah blah blah
>
> i need to write something after "something="
> for example... i need to get this:
>
> something=
> aaaaaaa aaaaaaa aaaaaaaaa aaaaaaa aaaaaaaa
> blah blah blah
>
> i know how to add something on the end of the file but i dont know how to
> place it between "something=" and "blah blah blah"
<?php
$str='This is the new line!'."\n";
$file=dirname(__FILE__).'/Edit2.txt';
$contents=file($file);
foreach($contents as $id=>$line)
if(preg_match('/^something=(\r\n|\r|\n)$/',$line))
break;
$ar_end=array_splice($contents,$id+1);
$contents[]=$str;
$contents=array_merge($contents,$ar_end);
$fp=fopen($file,'w');
foreach($contents as $line)
fwrite($fp,$line,strlen($line));
fclose($fp);
?>
This will make sure that the the full line is matched, not just part of
it (in case you have "someting=" in another line as well). Not as
graceful as a simple:
preg_replace('/^(something=(\r\n|\r|\n))$/',"$1\n$str",$subject);
But I needed this exact functionality for something else. (I stripped
all the error-checking stuff to avoid confusion.)
--
Justin Koivisto - spam.RemoveThis@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.<!-- ~MESSAGE_AFTER~ -->
>> Stay informed about: php + txt