Skip to content

Instantly share code, notes, and snippets.

@fascinated
Last active December 30, 2021 17:03
Show Gist options
  • Save fascinated/299dfb9393bb409ba1a4f3c10e8db384 to your computer and use it in GitHub Desktop.
Save fascinated/299dfb9393bb409ba1a4f3c10e8db384 to your computer and use it in GitHub Desktop.
This script fixes missing newlines in a Movable Type export file
<?php
// -----------------------------
// When Movable Type (for example, version 4.32) exports posts, the files
// have \n instead of <br> in the HTML bodies of posts and comments to
// represent newlines. This confuses WordPress and corrupts the import process.
//
// This script replaces the double-newlines in post/comment bodies with <br><br>
// so that a WordPress import can proceed correctly
//
// To use, run "php fix_newlines.php mt-export.txt > mt-export-newlines.txt" in your terminal
// -----------------------------
if(!isset($argv[1])) { die("Usage: ./fix_newlines.php [filename]\n"); }
if(!file_exists($argv[1])) { die("Could not open " . $argv[1] . "\n"); }
$handle = fopen($argv[1], 'r');
if ($handle == null) { die("Could not open " . $argv[1] . "\n"); }
$context = '';
while ($line = fgets($handle)) {
$line = trim($line);
if ( '-----' == $line ) { // new sub-section
$context = '';
} else if ($line == '--------') { // new post section
$context = '';
} else if ($line == 'BODY:') {
$context = 'body';
} else if ($line == 'COMMENT:') {
$context = 'comment';
} else if ($context=="body" || $context=="comment") {
if(empty($line)) { $line = "<br><br>"; }
} else {
}
echo $line . "\n";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment