Re: Help seeking in a file a specific string
- From: Kim André Akerø <kimandre@xxxxxxxxxxxxxxxxxx>
- Date: 28 Oct 2005 12:45:31 GMT
Jorge A wrote:
> Hi!
>
> I want to read a file, and write something when a specific string is
> found.
>
> For instance I have this file:
>
> param1=32
> param2=2342
> param3=
> param4=23423
> param5=dfad
>
> I want to write after 'param3'. I tried with this but it doesn't work.
>
> $fp = fopen($file,'r+');
> while(!feof($fp)){
> $buffer = fgets($fp,4096);
> if ($buffer == 'param3=')
> fwrite ($fp, '2323');
> }
>
> $buffer seems not to read 'param3'
>
> Thanks in advance!
You'd be better off reading the entire file into the buffer and then
manipulating the string before writing the result back to the file.
Functional example:
<?php
$buffer = file_get_contents($file);
$buffer = str_replace("param3=","param3=2323",$buffer);
$fp = fopen($file,"w");
fwrite($fp,$buffer);
fclose($fp);
?>
--
Kim André Akerø
- kimandre@xxxxxxxxxxxxxxxxxx
(remove NOSPAM to contact me directly)
.
- References:
- Help seeking in a file a specific string
- From: Jorge A
- Help seeking in a file a specific string
- Prev by Date: Re: Help seeking in a file a specific string
- Next by Date: Re: Help seeking in a file a specific string
- Previous by thread: Re: Help seeking in a file a specific string
- Next by thread: Re: Help seeking in a file a specific string
- Index(es):
Relevant Pages
|