Jump to content

read ,write and replace help


sasori

Recommended Posts

hi i am having problem with writing and replacing strings to a file

 

i have this 'testfile.txt' with the words

 

adsfadsfasdfadsfasdfadfasddfsasori
asdfadsfasdsasori
asasdfasdfsasori adsfadfsasori

 

now here's my php code

$file = 'testfile.txt';
$fh = fopen($file,'r+');
$data = fread($fh,filesize($file));
$string = "sasori";
$replace = "replacement";
$change = str_replace($string,$replace,$data);
fwrite($fh,$change);
fclose($fh);

 

whenever i run this on command prompt php.exe -f replace.php

the data on the text file just appends the replacement..it doesn't replace the original search string

 

adsfadsfasdfadsfasdfadfasddfsasori
asdfadsfasdsasori
asasdfasdfsasori adsfadfsasoriadsfadsfasdfadsfasdfadfasddfreplacement
asdfadsfasdreplacement
asasdfasdfreplacement adsfadfreplacement

 

please tell me what to do in order to replace also those other strings

 

Link to comment
https://forums.phpfreaks.com/topic/167371-read-write-and-replace-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.