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
- Weeknotes: Llama 3, AI for Data Journalism, llm-evals and datasette-secrets - 23rd April 2024
- Options for accessing Llama 3 from the terminal using LLM - 22nd April 2024
- AI for Data Journalism: demonstrating what we can do with this stuff right now - 17th April 2024
- Three major LLM releases in 24 hours (plus weeknotes) - 10th April 2024
- Building files-to-prompt entirely using Claude 3 Opus - 8th April 2024
- Running OCR against PDFs and images directly in your browser - 30th March 2024
- 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