neat_string.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: neat__string_8php-source.html 675 2008-12-26 00:27:14Z gwoo $ */
00003 /**
00004  * String handling methods.
00005  *
00006  * Random passwords, splitting strings into arrays, removing Cyrillic characters, stripping whitespace.
00007  *
00008  * PHP versions 4 and 5
00009  *
00010  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00011  * Copyright 2005-2008, Cake Software Foundation, Inc.
00012  *          1785 E. Sahara Avenue, Suite 490-204
00013  *          Las Vegas, Nevada 89104
00014  *
00015  * Licensed under The MIT License
00016  * Redistributions of files must retain the above copyright notice.
00017  *
00018  * @filesource
00019  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00020  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00021  * @package         cake
00022  * @subpackage      cake.cake.libs
00023  * @since           CakePHP(tm) v 0.2.9
00024  * @version         $Revision: 675 $
00025  * @modifiedby      $LastChangedBy: gwoo $
00026  * @lastmodified    $Date: 2008-12-25 16:27:14 -0800 (Thu, 25 Dec 2008) $
00027  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00028  */
00029 /**
00030  * String handling methods.
00031  *
00032  * Random passwords, splitting strings into arrays, removing Cyrillic characters, stripping whitespace.
00033  *
00034  * @package     cake
00035  * @subpackage  cake.cake.libs
00036  */
00037 class NeatString{
00038 /**
00039  * Returns an array with each of the non-empty characters in $string as an element.
00040  *
00041  * @param string $string
00042  * @return array
00043  */
00044     function toArray($string) {
00045         $split = preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY);
00046         return $split;
00047     }
00048 /**
00049  * Returns string with Cyrillic characters translated to Roman ones.
00050  *
00051  * @param string $string
00052  * @return string
00053  */
00054     function toRoman($string) {
00055         $pl = array('ą','ć','ę','ł','ń','ó','ś','ź','ż','Ą','Ć','Ę','�?','Ń','Ó','Ś','Ź','Ż');
00056         $ro = array('a','c','e','l','n','o','s','z','z','A','C','E','L','N','O','S','Z','Z');
00057         $replace = str_replace($pl, $ro, $string);
00058         return $replace;
00059     }
00060 /**
00061  * Returns string as lowercase with whitespace removed.
00062  *
00063  * @param string $string
00064  * @return string
00065  */
00066     function toCompressed($string) {
00067         $whitespace = array("\n", " ", "\r", "\0", "\x0B", " ");
00068         $replace = strtolower(str_replace($whitespace, '', $string));
00069         return $replace;
00070     }
00071 /**
00072  * Returns a random password.
00073  *
00074  * @param integer $length Length of generated password
00075  * @param string $available_chars List of characters to use in password
00076  * @return string Generated password
00077  */
00078     function randomPassword($length, $available_chars = 'ABDEFHKMNPRTWXYABDEFHKMNPRTWXY23456789') {
00079         $chars = preg_split('//', $available_chars, -1, PREG_SPLIT_NO_EMPTY);
00080         $char_count = count($chars);
00081         $out = '';
00082         for ($ii = 0; $ii < $length; $ii++) {
00083             $out .= $chars[rand(1, $char_count)-1];
00084         }
00085         return $out;
00086     }
00087 }
00088 ?>