Skip to content

Instantly share code, notes, and snippets.

@drvy
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drvy/7c21777e4e9586646119 to your computer and use it in GitHub Desktop.
Save drvy/7c21777e4e9586646119 to your computer and use it in GitHub Desktop.
This script will replace all the emails in a wordpress .xml export file with an in-existing email in order to preserve the users privacy while being able to change the comment system of your wordpress or export the comments to somewhere else.
<?php
/* Config */
$xmlfile = 'wordpress-export-file.xml';
$ignore = array('your@email.com');
$replacement = 'somethingelse{num}@dev.null'; // {num} will be replaced with an automatic count..
$outputfile = 'output.xml';
$file = fopen($xmlfile,'r');
$content = fread($file,filesize($xmlfile));
fclose($file);
preg_match_all("/comment_author_email\>([a-zA-Z0-9\_\-\@\.]+)\<\/wp\:comment_author_email\>/mi", $content, $output);
if(!isset($content[1])){ die(' - No emails or comments found.'.PHP_EOL); }
$emails = array_unique($output[1],SORT_STRING);
$email_count = count($emails);
echo '- Total emails found: ',$email_count,PHP_EOL;
echo '- Starting replacement...',PHP_EOL;
$i = 0;
foreach($emails as $index=>$email){
if(!empty($email) && !in_array($email, $ignore)){
$new_mail = str_replace('{num}',$i,$replacement);
$content = str_replace($email,$new_mail,$content);
echo '-- Replacing: ',$email,' with: ',$new_mail,PHP_EOL;
} else { echo '-- Ignoring: ',$email, ' (Its empty or in the ignore list).',PHP_EOL; }
++$i;
unset($emails[$index]);
}
echo '- Replacement finalized. Total replaces: ',$i,PHP_EOL;
echo '- Saving outputfile: ',$outputfile,PHP_EOL;
$backup = fopen($outputfile,'w+');
fwrite($backup,$content);
fclose($backup);
echo '- Done',PHP_EOL;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment