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.

5 thoughts on “PHP Acronym Definer

  1. This is quite a cool function, definitely a worthwhile piece of code despite its age. I run a slightly modified version of it (extra acronyms, conversion to an actual plugin) on my WordPress install, and it seems to do the trick pretty flawlessly.

    Nicely done, Matt πŸ™‚

  2. Not trying to be a smartass or anything, but it’s “America Online”, not “American Online”. Just thought I would point that out, for those who use it, and for some crazy reason say “AOL” in their pages. Even though it’s like the worst curse word ever πŸ˜‰

  3. Not trying to be a smartass or anything (2), but it’s “Compact Disc”, not “Compact Disk”. Personally, I don’t get the idea of such scripts since one can Google such acronyms in a few seconds. It’s slowing down your script and I doubt anybody will ever notice the extra bits of information.

  4. Great snippet… couple notes for anyone who cares:

    DVD = Digital Versatile Disc (not Video)
    RSS = Really Simple Syndication (Rich Site Summary is not at the forefront of usage)

    these used to be correct/accepted, but now-a-days these updates are petty standard.

  5. Another great tid-bit. Every specialized site (technology, health, etc..) should parse for acronym’s because most users are to lazy to open google to look for them. It has to b[i|y]te them if you want them to see it. And no, it doesn’t slow down the script if the replace is done on the submission of the post and not on each viewing.

SHARE YOUR THOUGHTS