Skip to content

Instantly share code, notes, and snippets.

@Ramoonus
Created August 21, 2011 13:24
Show Gist options
  • Save Ramoonus/1160610 to your computer and use it in GitHub Desktop.
Save Ramoonus/1160610 to your computer and use it in GitHub Desktop.
Function which uses regular expression to parse Microsoft Word footnotes into WordPress's Simple Footnotes format
/**
* Function which uses regular expression to parse Microsoft Word footnotes
* into WordPress's Simple Footnotes format
*
* @param string $content post content from filter hook
* @returns string post content with parsed footnotes
* @link http://ben.balter.com/2011/03/20/regular-expression-to-parse-word-style-footnotes/
*/
function bb_parse_footnotes( $content ) {
global $post;
if ( !isset( $post ) )
return;
//if we have already parsed, kick
if ( get_post_meta($post->ID, 'parsed_footnotes') )
return $content;
$content = stripslashes( $content );
//grab all the Word-style footnotes into an array
$pattern = '#\<a href\="\#_ftnref([0-9]+)"\>\[([0-9]+)\]\</a\> (.*)#';
preg_match_all( $pattern, $content, $footnotes, PREG_SET_ORDER);
//build find and replace arrays
foreach ($footnotes as $footnote) {
$find[] = '#\<a href\="\#_ftn'.$footnote[1].'"\>\['.$footnote[1].'\]\</a\>#';
$replace[] = '[[ref]' . str_replace( array("\r\n", "\r", "\n"), "", $footnote[3]) . '[/ref]]';
}
//remove all the original footnotes when done
$find[] = '#\<div\>\s*<a href\="\#_ftnref([0-9]+)"\>\[([0-9]+)\]\</a\> (.*)\s*\</div\>\s+#';
$replace[] = '';
//make the switch
$content = preg_replace( $find, $replace, $content );
//add meta so we know it has been parsed
add_post_meta($post->ID,'parsed_footnotes', true, true);
return addslashes($content);
}
add_filter( 'content_save_pre', 'bb_parse_footnotes' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment