GetSimple Support Forum

Full Version: String find/replace script - Moving/migrating sites
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
(first posted here)

Here's a simple script (not a plugin, so no backend, sorry) to search/replace old->new site URLs in all files in a folder (e.g. *.xml in data/pages)

Set it up by editing the values in the first lines in the script (create the destination folder if it doesn't exist), save it as convertsiteurl.php (or what you wish) and open it in your browser like YOURSITE/convertsiteurl.php

The destination folder can be the same as the source, but just in case make a backup of all your date (well, better make a backup anyway).

PHP Code:
<?php
# setup values:
###############
$OLDSITEURL 'http://localhost/devsite/';
$NEWSITEURL 'http://www.example.com/';
$SOURCE 'data/pages/';
$DEST 'data/convertedfiles/';
$WRITEALL false// if true, write all files, not only updated ones
###############
# end setup

# process files
$SOURCE rtrim($SOURCE,'/').'/';
$DEST rtrim($DEST,'/').'/';
$OLDSITEURL rtrim($OLDSITEURL,'/').'/';
$NEWSITEURL rtrim($NEWSITEURL,'/').'/';
if (
$handle opendir($SOURCE)) {
  
$files = array();
  while (
false !== ($file readdir($handle))) {
    if (
strpos($file".") != 0){
      
$files[] = $file;
    }
  }
  
closedir($handle);
  echo 
count($files),' files:<br />';
  foreach (
$files as $filename){
    if (
$text file_get_contents($SOURCE.$filename)) {
      echo 
$filename,' <b>';
      
$found = (strpos($text$OLDSITEURL) !== false);
      if (
$found) {
        
$text str_replace($OLDSITEURL$NEWSITEURL$text);
        echo 
' - found';
      }
      if (
$WRITEALL or $found) {
        
file_put_contents($DEST.$filename$text);
        echo 
' - written';
      }
      echo 
'</b><br />';
    }
  }
}

?>

It can be used with the data/pages folder, but also with News Manager's data/posts, GS Blog's data/blog, etc.