Skip to main content

Regular Expression to Parse Word-style Footnotes into WordPress's Simple Footnotes Format

3 min read
TL;DR: Regular Expression to automatically parse Microsoft Word's footnote format into a more web-friendly format for WordPress's Simple Footnotes plugin
❗ Heads up! This post is archived and here for historical purposes. It may no longer be accurate or reflect my views. Proceed at your own risk.

I needed a quick-and-easy way to parse Microsoft Word’s footnote format into a more web-friendly format for a recent project. After a bit of regular expression hacking, I was able to build a WordPress plugin to automatically convert content pasted from Word into a format readable by Andrew Nacin’s popular Simple Footnotes plugin.

The process is surprisingly simple given WordPress’s extensive filter API. First, to grab the footnotes from Word’s ftnref format:

<?php
//grab all the Word-style footnotes into an array
$pattern = '#&lt;a href\\="#_ftnref([0–9-]+)">[([0–9-]+)]</a> (.\*)#';
preg_match_all( $pattern, $content, $footnotes, PREG_SET_ORDER);
?>

This creates an array ($footnotes) with the both the footnote number and the text of the footnote. We then need a way to replace the in-text reference with the parsed footnotes so that Simple Footnotes can understand them. I did this by creating two arrays, a find array and a replace array with each Word-style footnote reference and its Simple Footnote formatted counterpart:

<?php
//build find and replace arrays
foreach ($footnotes as $footnote) {
$find[] = '#&lt;a href\\="#_ftn'.$footnote[1].'">['.$footnote[1].']</a>#';
$replace[] = '[ref]' . str_replace( array("\\r\\n", "\\r", "\\n"), "", $footnote[3]). '[/ref]';
}
?>

Finally, so that the entire replacement can be done in a single pass, push a final find/replace pair into the end of the array, to remove the original footnotes:

<?php
//remove all the original footnotes when done
$find[] = '#<div>\s*<a href\="\#_ftnref([0-9]+)">[([0-9]+)\]</a> (.*)\s*</div>\s+#';
$replace[] = '';
?>

Because PHP’s preg_replace function can handle arrays, all we have to do is run a single function:

<?php
$content = preg_replace( $find, $replace, $content );
?>

You can find the full plugin code in the GitHub repository.

To use, you can download the plugin file1 and activate (be sure you already have Simple Footnotes installed). Copy the content from Word, and Paste into the “Paste from Word” box (may need to toggle the “Kitchen Sink”.2

Thoughts? Improvements? The above code solved a rather stubborn workflow problem in a project I was working on, and hopefully it can do the same for you. Feel free to use/improve the above code.

Footnotes#

  1. Licensed under GPLv2 ↩

  2. You can even Fork the plugin over on GitHub ↩

Originally published March 20, 2011 View revision history
Share

Related articles

More to explore

How I over-engineered my book

23 min read

My book has a linter, ~5,500 automated checks, and a Pandoc pipeline that rebuilds five formats (EPUB, Kindle, paperback ×2, and web) on every git push. For a book one person wrote. Here's how I built and published it the way I ship software.

We've been trained to make paper

5 min read

If the internet is the primary medium by which content is consumed, shouldn't that be the primary medium for which content is prepared?

Why WordPress

11 min read

The case for WordPress as the world's most versatile publishing platform, from its proven track record and ease of use to its community-driven ecosystem.

Ben Balter

I'm Ben Balter, and I write here about product, engineering leadership, open source, and showing your work. I wrote Open and Async, the playbook for remote and distributed teams. My open source projects have hundreds of millions of downloads. Most recently I was Director of Hubber Enablement at GitHub, where I owned the internal platform 4,000+ GitHubbers use to get their work done. Before that: Chief of Staff for Security, five years running trust and safety, and GitHub's first Government Evangelist. Before GitHub: attorney, Presidential Innovation Fellow, and member of the White House's first agile development team. More about the author →

Follow along: Bluesky LinkedIn

This page is open source Help improve this article on GitHub

Keyboard shortcuts

Search

  • / or ⌘ K Open search
  • ↓ or j ↑ or k Move through results
  • Enter Open the selected result
  • Esc Close search

Go to

  • g then h Home
  • g then p All posts

This page

  • y Copy link to this page
  • ? Show this list