Created
July 2, 2012 15:07
-
-
Save CashWilliams/3033702 to your computer and use it in GitHub Desktop.
regex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p>This is a post I'm digging up from an old blog of mine, but refreshed. I needed to remove the table prefix from an existing Drupal site. The site had tables in the form of <code>drup_TABLENAME</code> and needed to be in the standard Drupal format as <code>TABLENAME</code>. I wrote a quick php script which can be called from Drush.</p> | |
<p>The script does not delete any existing tables unless needed, and but will drop a table by the same name (without the prefix) if it exists before trying to rename it. The database I was working in was pretty dirty and had a fresh install mixed amongst the prefixed tables.</p> | |
<p>I've tested it and run the against the site using the following steps:</p> | |
<ol> | |
<li><p>Run the file with drush</p> | |
<pre><code>drush php-script fix_prefix.php | |
</code></pre></li> | |
<li><p>Remove the $db_prefix from settings.php</p></li> | |
<li><p>Use drush to clear all the caches</p> | |
<pre><code>drush cc all | |
</code></pre></li> | |
</ol> | |
<p><em>The script is also available as a <a href="http://gist.github.com/3015279">gist</a></em></p> | |
<hr /> | |
<p>fix_prefix.php:</p> | |
<pre><code> <?php | |
// current table prefix to be removed | |
$prefix = "drup_"; | |
// echo generated statments rather then run them | |
$pretend = FALSE; | |
///////////////////////////////////////////////////////////////////// | |
$table_list = db_query("SHOW TABLES"); | |
$prefix = strtolower($prefix); | |
foreach ($table_list as $r) { | |
$r = (array)$r; | |
$table_old = strtolower(current($r)); | |
// check for $prefix on this table | |
if(substr($table_old,0,strlen($prefix)) == $prefix) { | |
$table_new = substr($table_old, strlen($prefix)); | |
// first drop $table_new incase it already exists | |
$clean_sql = "DROP TABLE IF EXISTS {$table_new}"; | |
// rename prefix table to standard/nonprefix name | |
$rename_sql = "RENAME TABLE {$table_old} TO {$table_new}"; | |
if($pretend) { | |
print $clean_sql."\n"; | |
print $rename_sql."\n"; | |
} else { | |
if(!db_query($clean_sql)) { | |
die("Aborting - $clean_sql \n"); | |
} | |
if(!db_query($rename_sql)) { | |
die("Aborting - $rename_sql \n"); | |
} | |
} | |
} else { | |
print "$table_old skipped \n"; | |
} | |
} | |
print "\nDone \n\n"; | |
?> | |
</code></pre> | |
php script searching for above - | |
$pattern = '#\\[geshifilter-('. $tags_string .')([^\\]]*)\\](.*?)(\\[/geshifilter-\1\\])#s'; | |
$text = preg_replace_callback($pattern, create_function('$match', "return _geshifilter_replace_callback(\$match, '$format');"), $text); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment