Wednesday, May 27, 2009

Simple Find and Replace PHP Script

PHP Quick Find Replace Script

I was working on a client who had an older website with about 100 pages and they were all “full html.” By full html I mean the entire layout was stored in every html file, so any change to the layout required a lot of time. So I created a quick and dirty script to load into the directory I was working in, it pulls all files with the defined extension then finds and replaces one string for another.

This script is very easy to setup and run.


a. $ext = “php” – Set this to whatever extension you wish to edit. Currently it is only setup to do one extension at a time. I usually run it on php and then the html files, then css if urls needs to be changed.

b. $current = “123456” – Set this to whatever your pages currently say. This is what you want to replace/get rid of.

c. $new = “ABCD” – Set this to whatever is to replace $current.

d. An example of replacing to would be:

$ext = “html”
$current = “
$new = “

  • 4. Upload your edited replace.php to whatever directory contains the files you wish to edit
  • 5. Point your browser to:
  • 6. Repeat for any other extension
  • 7. Delete replace.php

Things that could be added later:

GUI interface with password protection
Do more than one directory at a time
Do more than one file extension at a time
Create backup of files to be edited with an undo button

Hope this helps!

Salt Lake City Web Design

No comments:

Post a Comment