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 ); ?> Quote Link to comment 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 Quote Link to comment 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"; } ?> Quote Link to comment 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? Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.