Search and replace for characters in the whole database for Drupal

<?php
# Define which characters you want changed (key) and what you want them changed to (value)
$charChangeArray = array(
                           
'Ã¥'=>'å',
                           
'Ã…'=>'Å',
                           
'ö'=>'ö',
                           
'Ö'=>'Ö',
                           
'ä'=>'ä',
                           
'Ä'=>'Ä',
                           
'ç'=>'ç',
                           
'é'=>'é',
                           
'¿½'=>'è',
                           
'â'=>'â',
                           
'hô'=>'ô',  
                           
'ü'=>'ü',
                           
'ó'=>'ó',  
                           
'ú'=>'ú',
                           
'â€'=>'"',  
                           
'–'=>'-',
                           
'•'=>'&bull;',
                           
'ã'=>'ã',
                           
'á'=>'á',      
                           
'vÃ'=>'í',  
                           
'Å¡'=>'š'
                           
);


$sql = 'SHOW TABLES';
if ( !(
$result = db_query( $sql ) ) ) {
       echo
'<span style="color: red;">Get SHOW TABLE - SQL Error: <br>' . "</span>\n";
    }

 
while (
$tables = db_fetch_array($result) ) {
    echo
$tables[0];
    
# Loop through all tables in this database
      
$table = $tables[key($tables)];


  
# Now loop through all the fields within this table
  
if ( !($result2 = db_query("SHOW COLUMNS FROM %s",$table) ) ) {
          echo
'<span style="color: red;">Get Table Columns Query - SQL Error: <br>' . "</span>\n";
   
          break;
           }
  echo
"---- $table <br>\n";

   while (
$column = db_fetch_array( $result2 ) )
   {
     
$field_name = $column['Field'];
     
$field_type = $column['Type'];
   
     
# Change text based fields
     
$skipped_field_types = array('char', 'text', 'enum', 'set', 'blob');
   
      foreach (
$skipped_field_types as $type )
      {
  
         if (
strpos($field_type, $type) !== false )
         {
             
# Loop thru array for each character change
           
foreach ($charChangeArray as $key => $value)
            {
               
$query = "UPDATE $table SET $field_name = REPLACE ( $field_name, '$key', '$value' )";
               
$result3 = db_query ( $query );
            }
 
            echo
"---- $field_name was successfully characterized.<br>\n";
         }
      }
   }
   echo
"<hr>\n";
}
?>

Reply

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

urchinTracker(); // -->