Location: PHPKode > projects > Smileys > smileys/Smileys.php
<?php
////////////////////////////////////////////////////////////////
/*

This class replaces smiley tags with smiley images.
Useful for forums or guestbooks.
When created it reads the smiley definitions and can be called afterwards.


For the lastest version go to:
http://www.phpclasses.org/browse.html/package/813.html
    
FUNCTIONS:
    function smileys($url)
    function getSmileys()
    function insertSmileys($str)

    function _resetSmileys($str)


For smileys see: 
    http://www.web-kun.de/Forum-Smileys/bunt.htm 
    

////////////////////////////////////////////////////////////////

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.
    
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
    
    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
////////////////////////////////////////////////////////////////
/**
* This class replaces smileys tags with smiley images.
* Useful for forums or guestbooks.
*
* @author	    Lennart Groetzbach <hide@address.com>
* @copyright	Lennart Groetzbach <hide@address.com> - distributed under the LGPL
* @version 	    0.81 BETA - 2002/10/01
* 
* <p>
* History / Changes<br>
* <table border="1" width="100%" cellpadding="3"><tr>
*       <th>Version</th>    <th>Reported by</th>        <th>File / Function Changed</th>    <th>Date of Change</th> <th>Commment</th>
* </tr><tr>
*       <td>0.81</td>        <td>H.Rettinger</td>        <td>_resetSmileys()</td>            <td>2002/10/20</td>     <td>error in if clause</td>
* </tr></table>
*
* @access   public
*/
class Smileys {

////////////////////////////////////////////////////////////////
/**
* Contains the smiley information
* 
* @access   private
* @type     Array
*/
var $_smileys = array();
/**
* The generated error messages, line feed seperated
* 
* @access   public
* @type     String
*/
var $error_message = '';

////////////////////////////////////////////////////////////////////////
/**
* Constructor
*
* Creates a new smileys class from the smiley definition
*
* @access   public
*
* @param    String      $url   url to the smiley definition file
*
* @return   int         -1 if error occurs
*/
function smileys($url) {
    $this->_resetSmileys($url);
}

////////////////////////////////////////////////////////////////////////
/**
* Adds the smileys to a string
*
* Adds the html tags for each smiley code found
*
* @access   public
* @param    String      $str
* @return   String  
*/
function insertSmileys($str) {
    // for each smileys
    for ($i=0; $i < sizeof($this->_smileys); $i++) {
        $smiley = $this->_smileys[$i][0];
        $image = $this->_smileys[$i][1];
        $x = $this->_smileys[$i][2];
        $y = $this->_smileys[$i][3];
	    $replace = "<img src=\"$image\" alt=\"$smiley\" width=\"$x\" height=\"$y\" border=\"0\">";
        // replace it with the image tag
    	$str = str_replace($smiley, $replace, $str);
	}
	return $str;
}

////////////////////////////////////////////////////////////////////////
/**
* Returns all smileys
*
* Returns an array of arrays.
*
* @access   public
* @return   Array 
*       One entry contains the smiley tag, the image path, the width and the height.
*
*/

function getSmileys() {
    return $this->_smileys;
}

////////////////////////////////////////////////////////////////////////
/**
* Parses the smileys definition file
*
* Parses the smileys definition file into the _smileys array
*
* @access   private
* @param    String      $url   the smileys definition file
* @return   int         -1 if error occurs
*/

function _resetSmileys($url) {
    // erase old list
    $this->_smileys = array();
	// open smileys definition
	$fp = @fopen($url, "r");
	// does the file exist?
	if (@$fp) {
		while (!feof ($fp)) {
		    // read one line
			$buffer = fgets($fp, 4096);
			// is it not a comment or emtpy
	    	if ((strpos($buffer, "##") == false) && (ord($buffer) > 31)) {
				$buffer = str_replace("\n", "", $buffer);
		    	// get the definition
		    	$list = explode("\t", $buffer);
            	// get the size
            	$size = @GetImageSize($list[1]);
            	// is it an image?
            	if ($size != false) {
                    $list[2] = $size[0];
                    $list[3] = $size[1];
                    // add the definition to the smileys array
                    array_push($this->_smileys, $list);
            	}
			}
		}
		fclose ($fp);
	} else {
	    $this->error_message .= "resetsmileys(): Error opening file!\n";
	    return -1;
	}
}

////////////////////////////////////////////////////////////////////////
}
////////////////////////////////////////////////////////////////////////

?>
Return current item: Smileys