You are here: Re: Help seeking in a file a specific string « All PHP « IT news, forums, messages
Re: Help seeking in a file a specific string

Posted by Kim Andrι Akerψ on 10/13/31 11:30

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@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)

 

Navigation:

[Reply to this message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация