Location: PHPKode > scripts > Talk Leet > talk-leet/classes/talkleet.class.php
<?php
/**
 * @name Talk Leet
 * @version 1.0
 * @author David Sopas Ferreira <coder at davidsopas dot com>
 * @copyright 2008
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * --------------------------
 * more at www.davidsopas.com
 * 
 */

class talkleet
{
    /**
     * You can add more if you want, but remember to when adding on $original_letters, 
     * add also it's equivalent in $leet_letters array
     */
    var $original_letters = array("a", "e", "the", "s", "S", "A", "o", "O", "t", "l",
        "ph", "y", "H", "W", "M", "D", "V", "x");
    var $leet_letters = array("4", "3", "+eh", "z", "Z", "4", "0", "0", "+", "1",
        "f", "j", "|-|", "\\/\\/", "|\\/|", "|)", "\\/", "><");

    /**
     * shows rate or not (1 - show, 0 - doesnt show)
     */
    var $showrate = 1;

    var $result = "";
    var $counter = 0;

    /**
     * You have more options to set on $err_rep, check http://php.net/manual/en/function.error-reporting.php
     */
    var $err_rep = E_ALL;

    /**
     * Initialization of error reporting (__constructor)
     */
    function Talkleet($disable_error_reporting = true)
    {
        if ($disable_error_reporting)
        {
            $this->err_rep = @error_reporting(E_ERROR);
        } else
        {
            $this->err_rep = @error_reporting();
        }
    }

    /**
     * Encode normal text to leet text
     * @param $string
     * @return string
     */
    function encode($string)
    {
        $counter = $this->counter++;
        for ($i = 0; $i < strlen($string); $i++)
        {
            /**
             * $char keeps now the letter
             */
            $char = $string[$i];

            /**
             * Condition to check if the letter is present in the array of the original words
             */
            if (in_array($char, $this->original_letters))
            {
                /**
                 * It's present. Now we need the array key to use it on the leet array and grab the value from it
                 */
                $searchletter_key = array_search($char, $this->original_letters);
                $leet_convert = str_replace($char, $this->leet_letters[$searchletter_key], $char);
                $counter++;
            } else
            {
                /**
                 * If the letter it's not present it remains unchanged
                 */
                $leet_convert = $char;
            }
            $result .= $leet_convert;
        }

        /**
         * Removes spaces because they are not needed for counting chars for rateleet()
         */
        $spaces_notcount = str_replace(" ", "", $string);
        
        /**
         * Rate leet value is given by math third simple rule ( 100% x number of changed letters) / total letters
         */
        $rate = round((100 * ($counter-1)) / strlen($spaces_notcount), 0);

        /**
         * Returns the result with optional leetness rate
         */
        if ($this->showrate == 1)
            $rate = " (Leetness rate: " . $rate . "%)";
        elseif ($this->showrate == 0)
            $rate = "";
        else
            $rate = "Option not available";

        $result = $result . $rate;

        return $result;
    }

    /**
     * Decode leet text to normal text
     * @param $string
     * @return string
     */
    function decode($string)
    {
        /**
         * Runs all over the string and replace any leet letters array match for original letters array 
         */
        for ($key = 0; $key < count($this->leet_letters); $key++)
        {
            $string = str_replace($this->leet_letters[$key], $this->original_letters[$key],
                $string);
        }

        $result = $string;
        return $result;
    }

    /**
     * Destroy the object
     */
    function __destruct()
    {
        @error_reporting($this->err_rep);
    }
}

?>
Return current item: Talk Leet