Skip to content

Instantly share code, notes, and snippets.

@mohamedmansour
Created August 19, 2011 19:06
Show Gist options
  • Save mohamedmansour/1157712 to your computer and use it in GitHub Desktop.
Save mohamedmansour/1157712 to your computer and use it in GitHub Desktop.
Acronym Definition class to add acyronyms to the first occurring word in the Acronym list.
<?
/**
*
* PHP 5.1 Acronym Definition Class
* Adds acronymn tags to the first occuring word which is in the Acronym List
* Very nice package which allows your users to see what the abbreviated word is.
*
* @name
* DefinitionList
* @version
* 1.0
* @author
* Mohamed Mansour (http://mohamedmansour.com)
* @since
* August 9th, 2005
* @example
* Installation
* 1- Include this file to your page:
* require_once 'class.definition.inc.php';
*
* 2- Create the object in your page:
* $def = new DefinitionList();
*
* 4- Include Acronyms Dictionary
* DATABASE: require_once 'dbdictionary.inc.php';
* NONDB: require_once 'dictionary.inc.php';
* For adding a Definition:
* $def->AddAcronym(new TypeDefinition('.NET','Windows Programming Language'));
*
* 4- Use the functions
* Using the DefinitionList:
* $text = $def->check($text);
*
* @see
* Take a look at my website for more scripts
* Under GNU License
*
*/
class DefinitionList {
private $dictionary = array();
public $text;
private $current_type;
/**
*
* Add Acronymn to list of acronyms
*
* @param value
* the obect sent to TypeDefinition Class
*
*/
public function AddAcronym(TypeDefinition $value)
{
array_push($this->dictionary,$value);
}
/**
*
* Check the text if any acronyms exist and replace them
*
* @param text
* The text which is inputed to check for acronyms
* @return
* String
*
*/
public function check($text)
{
$types = $definitions = array();
foreach ($this->dictionary as $term) {
$types[] = $this->Convert($term->getType(),1);
$definitions[] = $this->Convert($term->getDef(),2);
}
$text = preg_replace($types, $definitions, $text, 1);
return $text;
}
/**
*
* Add the proper regex and Acronym tag to corresponding terms
*
* @param str
* @param type
* Which converstion would be used
* @return
* String
*
*/
private function Convert($str,$type)
{
switch ($type) {
case 2:
return "<acronym title=\"".$str."\">".$this->current_type."</acronym>";
default:
$this->current_type = $str;
$str = preg_quote($str);
return "/(?<=^|\s|(\s|\.)\.|^\.)$str(?=$|\s|\.$|\.(\s|\.)|\,)/i";
}
}
}
/**
*
* The type hinting portion of the upper class
* to force parameters to be objects or arrays.
*
*/
class TypeDefinition
{
// Class objects
private $definition;
private $type;
/**
*
* Constructor
*
* @param type
* sets the type
* @param def
* sets the definition of the type
*/
public function __construct($type, $def)
{
$this->type = $type;
$this->definition = $def;
}
/**
*
* Returns the current type of the object
*
* @return
* Object
*
*/
public function getType()
{
return $this->type;
}
/**
*
* Returns the current definition of the object
*
* @return
* Object
*
*/
public function getDef()
{
return $this->definition;
}
}
<?
// Loading the definition list
$def->addAcronym(new TypeDefinition('WWW','World Wide Web'));
$def->addAcronym(new TypeDefinition('W3C','World Wide Web Consortium'));
$def->addAcronym(new TypeDefinition('ASP','Active Server Pages'));
$def->addAcronym(new TypeDefinition('C','C Programming Language'));
$def->addAcronym(new TypeDefinition('MRC','MiRC Scripting'));
$def->addAcronym(new TypeDefinition('HTML','HyperText Markup Language'));
$def->addAcronym(new TypeDefinition('DOM','Document Object Model'));
$def->addAcronym(new TypeDefinition('CSS','Cascading Style Sheets'));
$def->addAcronym(new TypeDefinition('PHP','Hypertext Preprocessor'));
$def->addAcronym(new TypeDefinition('CPU','Central Processing Unit'));
$def->addAcronym(new TypeDefinition('RAM','Random Access Memory'));
$def->addAcronym(new TypeDefinition('ROM','Read Only Memory'));
$def->addAcronym(new TypeDefinition('CD','Compact Disc'));
$def->AddAcronym(new TypeDefinition('XHTML','eXtensible HyperText Markup Language'));
$def->AddAcronym(new TypeDefinition('XML','eXtensive Markup Langage'));
$def->AddAcronym(new TypeDefinition('FTP','File Transfer Protocol'));
$def->AddAcronym(new TypeDefinition('JPG','Joint Picture Expert Group'));
$def->AddAcronym(new TypeDefinition('JPEG','Joint Picture Expert Group'));
$def->AddAcronym(new TypeDefinition('PNG','Portable Network Graphics'));
$def->AddAcronym(new TypeDefinition('RSS','Rich Site Summary'));
$def->AddAcronym(new TypeDefinition('IE','Internet Explorer'));
$def->AddAcronym(new TypeDefinition('FF','Fire Fox Browser'));
$def->AddAcronym(new TypeDefinition('CMS','Control Managment System'));
$def->AddAcronym(new TypeDefinition('RSS','Really Simple Syndication'));
$def->AddAcronym(new TypeDefinition('.NET','Windows Programming Language'));
$def->AddAcronym(new TypeDefinition('WCAG','Web Content Accessibility Guidelines'));
$def->AddAcronym(new TypeDefinition('MVC','Model View Controller pattern'));
$def->AddAcronym(new TypeDefinition('SMARTY','PHP Templating library to seperate logic from design'));
$def->AddAcronym(new TypeDefinition('MYSQL','The biggest opensource database in the world'));
$def->AddAcronym(new TypeDefinition('AJAX','Asynchronous JavaScript and XML'));
$def->AddAcronym(new TypeDefinition('uced','Use Case Editor'));
$def->AddAcronym(new TypeDefinition('WebGL','Web Graphics Library'));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment