Location: PHPKode > scripts > Content Parser > content-parser/Smileys.php
<?php
/**
  * Model for parsing smileys.
  *
  * @author Nikola Posa, www.nikolaposa.in.rs
  */
class Model_ContentParser_Smileys extends ContentParser_Abstract
{
	/**
	  * Smiley sign - image combination.
	  *
	  * @var array
	  */
	protected $smileysImgs = array(
		':D' 		 => 'biggrin.gif',
		':s' 		 => 'confused.gif',
		':S' 		 => 'confused.gif',
		':8' 		 => 'cool.gif',
		':cry:' 	 => 'cry.gif',
		':eek:' 	 => 'eek.gif',
		':lol:' 	 => 'lol.gif',
		':mad:' 	 => 'mad.gif',
		':|' 		 => 'neutral.gif',
		':p' 		 => 'razz.gif',
		':P' 		 => 'razz.gif',
		':redface:'  => 'redface.gif',
		':rolleyes:' => 'rolleyes.gif',
		':(' 		 => 'sad.gif',
		':)' 		 => 'smile.gif',
		':o' 		 => 'surprised.gif',
		';)' 		 => 'wink.gif',
	);
	
	/**
	  * Flag which tells whether user choose to use smileys in his 
	  * webfolio site.
	  *
	  * @var bool
	  */
	protected $useSmileys;
	
	/**
	  * Constructor.
	  *
	  * @param mixed Content that need to be parsed.
	  * @param array Elements of $content that need to be parsed. (optional)
	  * @return ContentParser_Abstract
	  */
	public function __construct($content, $elements = array())
	{
		parent::__construct($content, $elements);
		
		$smileysFolder = Zend_Controller_Front::getInstance()->getBaseUrl() . '/front-end/img/smileys/';
		
		//Converting smiley images to <img /> tag.
		foreach ($this->smileysImgs as $smiley=>$img) {
			$this->smileysImgs[$smiley] = '<img src = "' . $smileysFolder . $img . '" alt = "' . $smiley . '" />';
		}
		
		$this->useSmileys = (bool)Model_WebfolioSettings::getInstance()->getSettings('use_smileys');
	}
	
	/**
	  * Static function that can be use for simplier access.
	  *
	  * @param mixed Content that need to be parsed.
	  * @param array Elements of $content that need to be parsed. (optional)
	  * @return mixed
	  */
	public static function parse($content, $elements = array())
	{
		//Creating instance of this class.
		$contentParser = new self($content, $elements);
		
		if ($contentParser->useSmileys == true) { //Smileys enabled?
			$contentParser->parseContent();
		}
		
		return $contentParser->content;
	}
	
	/**
	  * Defined by ContentParser_Abstract.
	  *
	  * @param string Text that will be parsed.
	  * @return string
	  */
	protected function doParseContent($text)
	{
		if (is_string($text)) {
			return str_replace(array_keys($this->smileysImgs), array_values($this->smileysImgs), $text);
		}
	}
}
?>
Return current item: Content Parser