How to Replace a URL and Save a File With PHP

  • Print this article
Use PHP to replace URLs in files.
Use PHP to replace URLs in files.

PHP (Hypertext Preprocessor) is a highly-versatile programming language. One use for this language is replacing of data in a file. With PHP, it is simple to scan documents and replace words, links and other data.



PHP can scan and replace data in any type of data file, such as HTML, text and other PHP files.

Things You'll Need

  • Programming tool (such as Notepad or Dreamweaver)
Show More

Instructions

    • 1

      Open a program to write an html file, such as Dreamweaver. Create a new file called "url.html" and copy the following data to it:

      <html>

      <body>

      My link list:

      http://www.demandstudios.com

      http://www.demandmedia.com

      </body>

      </html>

      Save the "url.html" file and put it on your website.

    • 2

      Open a program to write PHP code, such as Dreamweaver or Notepad. Create a new file called "replaceprogram.php" and copy the following code into it:

      <?

      //open&read url.html

      $myfile= fopen("url.html", "r");

      while (!feof($myfile)) {

      $newdata = $newdata.fgets($myfile);

      }

      fclose($myfile);

      //look for demandstudios.com URL and replace it with google.com

      $newdata = ereg_replace("http://www.demandstudios.com","http://www.google.com",$newdata);

      //save file

      $finaldata=stripslashes($newdata);

      $putthis = fopen("url.html","w");

      fputs($putthis,$finaldata);

      fclose($putthis);

      //done

      echo "replaced URL";

      ?>

      Save the "replaceprogram.php" file and put it on your website.

    • 3

      Go to "replaceprogram.php" on your website in a browser and the program will search through the "url.html" file, replace the specified URL and save the file.

Tips & Warnings

  • If this program doesn't work, contact your website host to make sure the site can run PHP code.

Related Searches

Comments

You May Also Like

Related Ads

Featured
View Mobile Site