Type: Full Script
Category: File Management
License: GNU General Public License
Description: VERY Simple = A script which will Find a chosen string and replace with a string within a file. ( Can be any file extention ) This script has two files in it. A html file called fp.html and a script called fp.php. The html file posts the string to find, the string to replace the find string with and a string which has in it the filename to write to.
//Underneath is the PHP script. Save it as fp.php //START OF PHP CODE <?php //These are the variables the html file will post to the fp.php script. $filename = $_POST['myFile'];; $tofind = $_POST['myFind'];; $toreplace = $_POST['myReplace'];; //Get the original contents of the filename you told it to find and give it a // variable called $file $file = file_get_contents($filename); //the Replace bit. Replaces the chosen find string the the chosen replace string in the original file contents of the filename you told it to go through. $end = str_replace($tofind, $toreplace, $file); //This bit is the Write script it opens the file you told it to edit. Write to it the new data with the replaced string in it. $fp = fopen($filename, "w"); //Open the filename and set the mode to Write if(fwrite($fp, $end)) ; //Write the New data to the opened file fclose($fp); //Close the File //Tell us what went on in the script. incase you made any errors in the find and replace strings. echo(" File name is $filename ... Finding $tofind .... Replacing with $toreplace ..... <br><br> Extras = File contants before was.... $file <br><br> .... <br><br> end results were $end"); ?> //END OF PHP CODE //BELOW IS THE FP.HTM FILE <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Replace and Find Script</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <!-- Replace http://WWW.YOURSITE.COM/fp.php With the URL to the Script. --> <form name="form1" method="post" action="http://WWW.YOURSITE.COM/fp.php"> <p> Find This <input name="myFind" type="text" id="myFind"> </p> <p> Replace with this <input name="myReplace" type="text" id="myReplace"> </p> <p> File name of the File to Edit <input name="myFile" type="text" id="myFile"> </p> <p> <input type="submit" name="Submit" value="Go"> </p> </form> </body> </html>