While moving Drupal or Wordpress sites, a common process is to export and import the database. When the domain changes there are usually lots of links to the old domain that need to be manually updated to point to the new domain, and sometimes it is hard to be sure that you have updated the domain everywhere. A simple replace from a text file editor will damage the serialized arrays from PHP, generating unpredictable results. This script automatically finds all references to the old domains and updates them to point to the new domain while preserving email accounts and fixing the string counts in the PHP´s serialized arrays. Some encoded versions of the old domains may remain after the process, but this is script is most likely going to make your life a lot easier.

This Script:

  • Fixes the string lengths in the serialized PHP arrays that are usually used to store settings.
  • Does not change email accounts
  • Changes HTML encoded URLs
  • The whole file is loaded in memory so take your RAM capacity into account.
  • Has only been tested with utf8.
  • Does not work in some older browses.

Try it:







The Code:

chdomain = function(sql, olds, newd) {
  var $ = jQuery;
  var reDomain = (/\d+:\\"(?:[^;]*[^\w\."@]|[^;]*%2F)?DOMAIN/g).source;
  var reSDomain = (/([^\w\.@]|%2F)DOMAIN/g).source;
  var rEsc = function(s) {return s.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); };

  for(var di in olds) {
    var d = $.trim(olds[di]);
    var dRegex = new RegExp(reDomain.replace('DOMAIN', rEsc(d)), 'g');
    var nmns = sql.match(dRegex);
    while(nmns && nmns.length) {
      for(var i in nmns) if(nmns[i].length){
        var parts = nmns[i].split(':\\"');
        parts[1] = parts[1].replace(d, newd);
        parts[0] = parseInt(parts[0]) + newd.length - d.length;
        var nnm = parts.join(':\\"');
        var cho = RegExp(rEsc(nmns[i]), 'g');
        console.log(cho.source, nnm);
        sql = sql.replace(cho, nnm);
      }
      nmns = sql.match(dRegex);
    }
    var cho2 = new RegExp(reSDomain.replace('DOMAIN',rEsc(d)), 'g');
    sql = sql.replace(cho2, '$1'+newd)	;
  }
  return sql;
};