Readme Posted June 5, 2010 Share Posted June 5, 2010 Takes the data from the file (.txt) and replace it - Saving changes? I searched the forum but did not find Bad code and slow <?php $string = 'My name is'; $patterns = 'My name is'; $replacements = 'is name my'; echo str_replace($patterns, $replacements, $string ); ?> Link to comment https://forums.phpfreaks.com/topic/203951-replace-in-txt/ Share on other sites More sharing options...
kenrbnsn Posted June 5, 2010 Share Posted June 5, 2010 Please explain more clearly what you want to do. Ken Link to comment https://forums.phpfreaks.com/topic/203951-replace-in-txt/#findComment-1068186 Share on other sites More sharing options...
Readme Posted June 5, 2010 Author Share Posted June 5, 2010 My English is not good but I try to explain. Now i want to use array function... can be used for multiple lines. This: $new_contents = str_replace("My name is", "is name my", $contents); Something like this: $contents= array( array(My name is, is name my), array(Hi,Hello), array(Come back, I coming)); <?php $file = 'textfile.txt'; if (file_exists($file)) { $ff = fopen($file, 'r+'); $contents = file_get_contents($file, FILE_TEXT); $new_contents = str_replace("My name is", "is name my", $contents); file_put_contents($file, $new_contents, FILE_TEXT); fclose($ff); $ff = fopen($file, 'r+'); fwrite($ff, $new_contents); fclose($ff); } else { echo "The file $filename does not exist"; } ?> Link to comment https://forums.phpfreaks.com/topic/203951-replace-in-txt/#findComment-1068201 Share on other sites More sharing options...
mraza Posted June 5, 2010 Share Posted June 5, 2010 <?php $file = 'textfile.txt'; if (file_exists($file)) { $ff = fopen($file, 'r+'); $contents = file_get_contents($file, FILE_TEXT); $change= array("My name is", "Hi", "Come back"); $replace = array("is name my","Hello","I coming"); $new_contents = str_replace($change, $replace, $contents); file_put_contents($file, $new_contents, FILE_TEXT); fclose($ff); $ff = fopen($file, 'r+'); fwrite($ff, $new_contents); fclose($ff); } else { echo "The file $filename does not exist"; } ?> something like this? Link to comment https://forums.phpfreaks.com/topic/203951-replace-in-txt/#findComment-1068212 Share on other sites More sharing options...
Readme Posted June 5, 2010 Author Share Posted June 5, 2010 No, mraza. Example! textfile.txt - words I coming The file filename does not exist Tired of these ads? Not work but sample $replace = array("I coming","1 2 3"); $replace = array("The file filename does not exist","1 2 3 4"); $replace = array("Tired of these ads?","1 2 3 4 5"); textfile.txt - now looks like this 1 2 3 1 2 3 4 1 2 3 4 5 Link to comment https://forums.phpfreaks.com/topic/203951-replace-in-txt/#findComment-1068243 Share on other sites More sharing options...
mraza Posted June 5, 2010 Share Posted June 5, 2010 sorry but i am not clear what you are trying to say, last script i assumed you have one file named "textfile.txt" within same location you are running this script , you wants to read that file and find some words and replace them, if so that should work, other then not clear to me Link to comment https://forums.phpfreaks.com/topic/203951-replace-in-txt/#findComment-1068246 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.