The pirate’s code
20th September 2003
So, now that “talk like a pirate” day has sadly come to an end, it’s time to reveal the five minute code hack that rendered my front page semi-legible for the best part of a day. It was actually pretty simple:
function piratify($text) {
return preg_replace_callback('/>(.*?)</s', 'aaaar', $text);
}
function aaaar($text) {
$text = $text[1];
$a = array(
'/\\bis\\b/' => 'be', # is => be
'/\\b([tT])he /' => "\\1'", # the => t'
'/\\bam\\b/' => 'be', # am => be
'/(\\w)v(\\w)/' => "\\1'\\2", # v => ' (in words)
'/ing\\b/' => "in'", # ing => in'
'/(\\w)ar(\\w)/' => "\\1aar\\2", # ar => aar (in words)
);
foreach($a as $re => $new) {
$text = preg_replace($re, $new, $text);
}
return '>'.$text.'<';
}
My first attempt simply applied the 6 regular expressions shown above, but they mangled links within my entries as well. The solution was to use preg_replace_callback to target only text occuring outside of HTML tags (defined as anything between a > and a <). This turned a five minute hack in to half an hour of frenzied debugging as I'd already posted the change to my site! In fact, the whole lot was written at 2am in the morning with my friend Tristan after a night out with Andy. Some how cider makes for easier construction of regular expressions.
I’m not the only person to have written a piratify function: Dougal Campbell has one as well (also mentioned here). I’m looking forward to seeing his released code.
More recent articles
- llm cmd undo last git commit - a new plugin for LLM - 26th March 2024
- Building and testing C extensions for SQLite with ChatGPT Code Interpreter - 23rd March 2024
- Claude and ChatGPT for ad-hoc sidequests - 22nd March 2024
- Weeknotes: the aftermath of NICAR - 16th March 2024
- The GPT-4 barrier has finally been broken - 8th March 2024
- Prompt injection and jailbreaking are not the same thing - 5th March 2024
- Interesting ideas in Observable Framework - 3rd March 2024
- Weeknotes: Getting ready for NICAR - 27th February 2024
- The killer app of Gemini Pro 1.5 is video - 21st February 2024
- Weeknotes: a Datasette release, an LLM release and a bunch of new plugins - 9th February 2024