Change line in a file


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function file_intersperse_contents($szFilename, $szInject, $szFind) {
  2.  
  3. // Check if file exist
  4. if(file_exists($szFilename)) {
  5.  
  6. // If file exist
  7. // Get the content
  8. $szContent = file_get_contents($szFilename);
  9.  
  10. // Replace the text
  11. $szReplace = str_replace($szFind, $szInject, $szContent);
  12.  
  13. // Open the file
  14. $szFile = fopen($szFilename, 'w+');
  15.  
  16. // Write the text into it
  17. $bWrite = fwrite($szFile, $szReplace);
  18.  
  19. if($bWrite) {
  20.  
  21. // If success, close file and return true
  22. fclose($szFile);
  23. return true;
  24.  
  25. }
  26.  
  27. // If failure, close file and return false
  28. fclose($szFile);
  29. return false;
  30.  
  31. }
  32.  
  33. else {
  34.  
  35. die('File' . $szFilename . 'does not exist!');
  36.  
  37. }
  38.  
  39. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.