PHP Acronym Definer

Released on December 21, 2002
Last Updated: December 21, 2002 2:23 AM
Version: 0.1

Description

When you run your text through this code it will define all the acronyms it can using the acronym tag. It also has a few other niceities, so check it out.

Installation/Usage

Pass whatever text you want to use through it, and add whatever acronyms you want to add to the array by copying what I have already. The sortr_longer must be above the acronymit function.

Code

Reverse Sort Array on Length

<?php
function sortr_longer($first, $second) {
return (strlen($first) < strlen($second)) ? 1 : -1;
}
?>

acronymit

<?php
function acronymit($text) {
    $acronyms = array(
'WYSIWYG' => 'what you see is what you get',
'XHTML' => 'eXtensible HyperText Markup Language',
'IIRC' => 'if I remember correctly',
'HDTV' => 'High Definition TeleVision',
'LGPL' => 'GNU Lesser General Public License',
'MSDN' => 'Microsoft Developer Network',
'WCAG' => 'Web Content Accessibility Guidelines',
'SOAP' => 'Simple Object Access Protocol',
'OPML' => 'Outline Processor Markup Language',
'MSIE' => 'Microsoft Internet Explorer',
'FOAF' => 'Friend of a Friend vocabulary',
'GFDL' => 'GNU Free Documentation License',
'XSLT' => 'eXtensible Stylesheet Language Transformation',
'HTML' => 'HyperText Markup Language',
'IHOP' => 'International House of Pancakes',
'IMAP' => 'Internet Message Access Protocol',
'RAID' => 'Redundant Array of Independent Disks',
'HPUG' => 'Houston Palm Users Group',
'VNC' => 'Virtual Network Computing',
'URL' => 'Uniform Resource Locator',
'W3C' => 'World Wide Web Consortium',
'MSN' => 'Microsoft Network',
'USB' => 'Universal Serial Bus',
'P2P' => 'Peer To Peer',
'PBS' => 'Public Broadcasting System',
'RSS' => 'Rich Site Summary',
'SIG' => 'Special Interest Group',
'RDF' => 'Resource Description Framework',
'AOL' => 'American Online',
'PHP' => 'PHP Hypertext Processor',
'SSN' => 'Social Security Number',
'JSP' => 'Java Server Pages',
'DOM' => 'Document Object Model',
'DTD' => 'Document Type Definition',
'DVD' => 'Digital Video Disc',
'DNS' => 'Domain Name System',
'CSS' => 'Cascading Style Sheets',
'CGI' => 'Common Gateway Interface',
'CMS' => 'Content Management System',
'FAQ' => 'Frequently Asked Questions',
'FSF' => 'Free Software Foundation',
'API' => 'Application Interface',
'PDF' => 'Portable Document Format',
'IIS' => 'Internet Infomation Server',
'XML' => 'eXtensible Markup Language',
'XSL' => 'eXtensible Stylesheet Language',
'GPL' => 'GNU General Public License',
'KDE' => 'K Desktop Environment',
'IE' => 'Internet Explorer',
'CD' => 'Compact Disk',
'GB' => 'Gigabyte',
'MB' => 'Megabyte',
'KB' => 'Kilobyte'
        );
    uksort($acronyms, 'sortr_longer'); // comment out if already sorted
    foreach ($acronyms as $acronym => $definition) {
        $text = preg_replace("#$acronym(?!</(ac|sp))#", "<acronym title=\"$definition\">$acronym</acronym>", $text, 1);
        $text = preg_replace("#$acronym(?!</(ac|sp))#", "<span class='caps'>$acronym</span>", $text);
    }
    return $text;
}
?>

Notes

You can speed it up a bit by commenting out the sort line if the array is in order of longest acronyms first. The reason for this is because the function goes down the array looking for that text to acronymfy, and it’ll grab whatever it comes to first. So if you have an acronym defined for LAMB and one for MB, if MB is first on the list, it will eat the last two letters of LAMB. To make people (mainly me) not have to sort it manually by acronym length I wrote a small function to reverse sort the array by the length of the key string.