示例#1
2
 public static function clean($var, $charset = NULL)
 {
     if (!$charset) {
         // Use the application character set
         $charset = JsonApiApplication::$charset;
     }
     if (is_array($var) or is_object($var)) {
         foreach ($var as $key => $val) {
             // Recursion!
             $var[UTF8::clean($key)] = UTF8::clean($val);
         }
     } elseif (is_string($var) and $var !== "") {
         // Remove control characters
         $var = UTF8::strip_ascii_ctrl($var);
         if (!UTF8::is_ascii($var)) {
             // Temporarily save the mb_substitute_character() value into a variable
             $mb_substitute_character = mb_substitute_character();
             // Disable substituting illegal characters with the default '?' character
             mb_substitute_character("none");
             // convert encoding, this is expensive, used when $var is not ASCII
             $var = mb_convert_encoding($var, $charset, $charset);
             // Reset mb_substitute_character() value back to the original setting
             mb_substitute_character($mb_substitute_character);
         }
     }
     return $var;
 }
示例#2
0
/**
 * UTF8::str_pad
 *
 * @package        Kohana
 * @author         Kohana Team
 * @copyright  (c) 2007-2012 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license        http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _str_pad($str, $final_str_length, $pad_str = ' ', $pad_type = STR_PAD_RIGHT)
{
    if (UTF8::is_ascii($str) and UTF8::is_ascii($pad_str)) {
        return str_pad($str, $final_str_length, $pad_str, $pad_type);
    }
    $str_length = UTF8::strlen($str);
    if ($final_str_length <= 0 or $final_str_length <= $str_length) {
        return $str;
    }
    $pad_str_length = UTF8::strlen($pad_str);
    $pad_length = $final_str_length - $str_length;
    if ($pad_type == STR_PAD_RIGHT) {
        $repeat = ceil($pad_length / $pad_str_length);
        return UTF8::substr($str . str_repeat($pad_str, $repeat), 0, $final_str_length);
    }
    if ($pad_type == STR_PAD_LEFT) {
        $repeat = ceil($pad_length / $pad_str_length);
        return UTF8::substr(str_repeat($pad_str, $repeat), 0, floor($pad_length)) . $str;
    }
    if ($pad_type == STR_PAD_BOTH) {
        $pad_length /= 2;
        $pad_length_left = floor($pad_length);
        $pad_length_right = ceil($pad_length);
        $repeat_left = ceil($pad_length_left / $pad_str_length);
        $repeat_right = ceil($pad_length_right / $pad_str_length);
        $pad_left = UTF8::substr(str_repeat($pad_str, $repeat_left), 0, $pad_length_left);
        $pad_right = UTF8::substr(str_repeat($pad_str, $repeat_right), 0, $pad_length_right);
        return $pad_left . $str . $pad_right;
    }
    throw new UTF8_Exception("UTF8::str_pad: Unknown padding type (:pad_type)", [':pad_type' => $pad_type]);
}
示例#3
0
/**
 * UTF8::strlen
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2011 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _strlen($str)
{
    if (UTF8::is_ascii($str)) {
        return strlen($str);
    }
    return strlen(utf8_decode($str));
}
示例#4
0
文件: str_pad.php 项目: azuya/Wi3
/**
 * UTF8::str_pad
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2010 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _str_pad($str, $final_str_length, $pad_str = ' ', $pad_type = STR_PAD_RIGHT)
{
    if (UTF8::is_ascii($str) and UTF8::is_ascii($pad_str)) {
        return str_pad($str, $final_str_length, $pad_str, $pad_type);
    }
    $str_length = UTF8::strlen($str);
    if ($final_str_length <= 0 or $final_str_length <= $str_length) {
        return $str;
    }
    $pad_str_length = UTF8::strlen($pad_str);
    $pad_length = $final_str_length - $str_length;
    if ($pad_type == STR_PAD_RIGHT) {
        $repeat = ceil($pad_length / $pad_str_length);
        return UTF8::substr($str . str_repeat($pad_str, $repeat), 0, $final_str_length);
    }
    if ($pad_type == STR_PAD_LEFT) {
        $repeat = ceil($pad_length / $pad_str_length);
        return UTF8::substr(str_repeat($pad_str, $repeat), 0, floor($pad_length)) . $str;
    }
    if ($pad_type == STR_PAD_BOTH) {
        $pad_length /= 2;
        $pad_length_left = floor($pad_length);
        $pad_length_right = ceil($pad_length);
        $repeat_left = ceil($pad_length_left / $pad_str_length);
        $repeat_right = ceil($pad_length_right / $pad_str_length);
        $pad_left = UTF8::substr(str_repeat($pad_str, $repeat_left), 0, $pad_length_left);
        $pad_right = UTF8::substr(str_repeat($pad_str, $repeat_right), 0, $pad_length_right);
        return $pad_left . $str . $pad_right;
    }
    trigger_error('UTF8::str_pad: Unknown padding type (' . $pad_type . ')', E_USER_ERROR);
}
示例#5
0
/**
 * UTF8::str_ireplace
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2010 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _str_ireplace($search, $replace, $str, & $count = NULL)
{
	if (UTF8::is_ascii($search) AND UTF8::is_ascii($replace) AND UTF8::is_ascii($str))
		return str_ireplace($search, $replace, $str, $count);

	if (is_array($str))
	{
		foreach ($str as $key => $val)
		{
			$str[$key] = UTF8::str_ireplace($search, $replace, $val, $count);
		}
		return $str;
	}

	if (is_array($search))
	{
		$keys = array_keys($search);

		foreach ($keys as $k)
		{
			if (is_array($replace))
			{
				if (array_key_exists($k, $replace))
				{
					$str = UTF8::str_ireplace($search[$k], $replace[$k], $str, $count);
				}
				else
				{
					$str = UTF8::str_ireplace($search[$k], '', $str, $count);
				}
			}
			else
			{
				$str = UTF8::str_ireplace($search[$k], $replace, $str, $count);
			}
		}
		return $str;
	}

	$search = UTF8::strtolower($search);
	$str_lower = UTF8::strtolower($str);

	$total_matched_strlen = 0;
	$i = 0;

	while (preg_match('/(.*?)'.preg_quote($search, '/').'/s', $str_lower, $matches))
	{
		$matched_strlen = strlen($matches[0]);
		$str_lower = substr($str_lower, $matched_strlen);

		$offset = $total_matched_strlen + strlen($matches[1]) + ($i * (strlen($replace) - 1));
		$str = substr_replace($str, $replace, $offset, strlen($search));

		$total_matched_strlen += $matched_strlen;
		$i++;
	}

	$count += $i;
	return $str;
}
示例#6
0
/**
 * UTF8::strrev
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2011 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _strrev($str)
{
    if (UTF8::is_ascii($str)) {
        return strrev($str);
    }
    preg_match_all('/./us', $str, $matches);
    return implode('', array_reverse($matches[0]));
}
示例#7
0
/**
 * UTF8::ucfirst
 *
 * @package        Kohana
 * @author         Kohana Team
 * @copyright  (c) 2007-2012 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license        http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _ucfirst($str)
{
    if (UTF8::is_ascii($str)) {
        return ucfirst($str);
    }
    preg_match('/^(.?)(.*)$/us', $str, $matches);
    return UTF8::strtoupper($matches[1]) . $matches[2];
}
示例#8
0
 public static function site($uri = '', $protocol = NULL, $index = TRUE)
 {
     $path = preg_replace('~^[-a-z0-9+.]++://[^/]++/?~', '', trim($uri, '/'));
     if (!UTF8::is_ascii($path)) {
         $path = preg_replace_callback('~([^/]+)~', 'URL::_rawurlencode_callback', $path);
     }
     return URL::base($protocol, $index) . $path;
 }
/**
 * UTF8::strcasecmp
 *
 * @package    JsonApiApplication
 * @author     JsonApiApplication Team
 * @copyright  (c) 2007-2012 JsonApiApplication Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _strcasecmp($str1, $str2)
{
    if (UTF8::is_ascii($str1) and UTF8::is_ascii($str2)) {
        return strcasecmp($str1, $str2);
    }
    $str1 = UTF8::strtolower($str1);
    $str2 = UTF8::strtolower($str2);
    return strcmp($str1, $str2);
}
示例#10
0
/**
 * UTF8::ucwords
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2012 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _ucwords($str)
{
    if (UTF8::is_ascii($str)) {
        return ucwords($str);
    }
    // [\x0c\x09\x0b\x0a\x0d\x20] matches form feeds, horizontal tabs, vertical tabs, linefeeds and carriage returns.
    // This corresponds to the definition of a 'word' defined at http://php.net/ucwords
    return preg_replace('/(?<=^|[\\x0c\\x09\\x0b\\x0a\\x0d\\x20])[^\\x0c\\x09\\x0b\\x0a\\x0d\\x20]/ue', 'UTF8::strtoupper(\'$0\')', $str);
}
示例#11
0
文件: url.php 项目: sysdevbol/entidad
 /**
  * Fetches an absolute site URL based on a URI segment.
  *
  *     echo URL::site('foo/bar');
  *
  * @param   string  $uri        Site URI to convert
  * @param   mixed   $protocol   Protocol string or [Request] class to use protocol from
  * @param   boolean $index		Include the index_page in the URL
  * @return  string
  * @uses    URL::base
  */
 public static function site($uri = '', $protocol = NULL, $index = TRUE)
 {
     // Chop off possible scheme, host, port, user and pass parts
     $path = preg_replace('~^[-a-z0-9+.]++://[^/]++/?~', '', trim($uri, '/'));
     if (!UTF8::is_ascii($path)) {
         // Encode all non-ASCII characters, as per RFC 1738
         $path = preg_replace('~([^/]+)~e', 'rawurlencode("$1")', $path);
     }
     // Concat the URL
     return URL::base($protocol, $index) . $path;
 }
示例#12
0
/**
 * UTF8::ltrim
 *
 * @package        Kohana
 * @author         Kohana Team
 * @copyright  (c) 2007-2012 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license        http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _ltrim($str, $charlist = null)
{
    if ($charlist === null) {
        return ltrim($str);
    }
    if (UTF8::is_ascii($charlist)) {
        return ltrim($str, $charlist);
    }
    $charlist = preg_replace('#[-\\[\\]:\\\\^/]#', '\\\\$0', $charlist);
    return preg_replace('/^[' . $charlist . ']+/u', '', $str);
}
示例#13
0
/**
 * UTF8::rtrim
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2012 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _rtrim($str, $charlist = NULL)
{
    if ($charlist === NULL) {
        return rtrim($str);
    }
    if (UTF8::is_ascii($charlist)) {
        return rtrim($str, $charlist);
    }
    $charlist = preg_replace('#[-\\[\\]:\\\\^/]#', '\\\\$0', $charlist);
    return preg_replace('/[' . $charlist . ']++$/uD', '', $str);
}
示例#14
0
/**
 * UTF8::substr_replace
 *
 * @package        Kohana
 * @author         Kohana Team
 * @copyright  (c) 2007-2012 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license        http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _substr_replace($str, $replacement, $offset, $length = null)
{
    if (UTF8::is_ascii($str)) {
        return $length === null ? substr_replace($str, $replacement, $offset) : substr_replace($str, $replacement, $offset, $length);
    }
    $length = $length === null ? UTF8::strlen($str) : (int) $length;
    preg_match_all('/./us', $str, $str_array);
    preg_match_all('/./us', $replacement, $replacement_array);
    array_splice($str_array[0], $offset, $length, $replacement_array[0]);
    return implode('', $str_array[0]);
}
示例#15
0
文件: ltrim.php 项目: nevermlnd/cv
/**
 * UTF8::ltrim
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2010 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _ltrim($str, $charlist = NULL)
{
	if ($charlist === NULL)
		return ltrim($str);

	if (UTF8::is_ascii($charlist))
		return ltrim($str, $charlist);

	$charlist = preg_replace('#[-\[\]:\\\\^/]#', '\\\\$0', $charlist);

	return preg_replace('/^['.$charlist.']+/u', '', $str);
}
示例#16
0
/**
 * UTF8::substr
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2012 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _substr($str, $offset, $length = NULL)
{
    if (UTF8::is_ascii($str)) {
        return $length === NULL ? substr($str, $offset) : substr($str, $offset, $length);
    }
    // Normalize params
    $str = (string) $str;
    $strlen = UTF8::strlen($str);
    $offset = (int) ($offset < 0) ? max(0, $strlen + $offset) : $offset;
    // Normalize to positive offset
    $length = $length === NULL ? NULL : (int) $length;
    // Impossible
    if ($length === 0 or $offset >= $strlen or $length < 0 and $length <= $offset - $strlen) {
        return '';
    }
    // Whole string
    if ($offset == 0 and ($length === NULL or $length >= $strlen)) {
        return $str;
    }
    // Build regex
    $regex = '^';
    // Create an offset expression
    if ($offset > 0) {
        // PCRE repeating quantifiers must be less than 65536, so repeat when necessary
        $x = (int) ($offset / 65535);
        $y = (int) ($offset % 65535);
        $regex .= $x == 0 ? '' : '(?:.{65535}){' . $x . '}';
        $regex .= $y == 0 ? '' : '.{' . $y . '}';
    }
    // Create a length expression
    if ($length === NULL) {
        $regex .= '(.*)';
        // No length set, grab it all
    } elseif ($length > 0) {
        // Reduce length so that it can't go beyond the end of the string
        $length = min($strlen - $offset, $length);
        $x = (int) ($length / 65535);
        $y = (int) ($length % 65535);
        $regex .= '(';
        $regex .= $x == 0 ? '' : '(?:.{65535}){' . $x . '}';
        $regex .= '.{' . $y . '})';
    } else {
        $x = (int) (-$length / 65535);
        $y = (int) (-$length % 65535);
        $regex .= '(.*)';
        $regex .= $x == 0 ? '' : '(?:.{65535}){' . $x . '}';
        $regex .= '.{' . $y . '}';
    }
    preg_match('/' . $regex . '/us', $str, $matches);
    return $matches[1];
}
示例#17
0
/**
 * UTF8::strpos
 *
 * @package        Kohana
 * @author         Kohana Team
 * @copyright  (c) 2007-2012 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license        http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _strpos($str, $search, $offset = 0)
{
    $offset = (int) $offset;
    if (UTF8::is_ascii($str) and UTF8::is_ascii($search)) {
        return strpos($str, $search, $offset);
    }
    if ($offset == 0) {
        $array = explode($search, $str, 2);
        return isset($array[1]) ? UTF8::strlen($array[0]) : false;
    }
    $str = UTF8::substr($str, $offset);
    $pos = UTF8::strpos($str, $search);
    return $pos === false ? false : $pos + $offset;
}
示例#18
0
/**
 * UTF8::strrpos
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2012 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _strrpos($str, $search, $offset = 0)
{
    $offset = (int) $offset;
    if (UTF8::is_ascii($str) and UTF8::is_ascii($search)) {
        return strrpos($str, $search, $offset);
    }
    if ($offset == 0) {
        $array = explode($search, $str, -1);
        return isset($array[0]) ? UTF8::strlen(implode($search, $array)) : FALSE;
    }
    $str = UTF8::substr($str, $offset);
    $pos = UTF8::strrpos($str, $search);
    return $pos === FALSE ? FALSE : $pos + $offset;
}
示例#19
0
/**
 * UTF8::str_split
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2012 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _str_split($str, $split_length = 1)
{
    $split_length = (int) $split_length;
    if (UTF8::is_ascii($str)) {
        return str_split($str, $split_length);
    }
    if ($split_length < 1) {
        return FALSE;
    }
    if (UTF8::strlen($str) <= $split_length) {
        return array($str);
    }
    preg_match_all('/.{' . $split_length . '}|[^\\x00]{1,' . $split_length . '}$/us', $str, $matches);
    return $matches[0];
}
示例#20
0
/**
 * UTF8::stristr
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2011 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _stristr($str, $search)
{
    if (UTF8::is_ascii($str) and UTF8::is_ascii($search)) {
        return stristr($str, $search);
    }
    if ($search == '') {
        return $str;
    }
    $str_lower = UTF8::strtolower($str);
    $search_lower = UTF8::strtolower($search);
    preg_match('/^(.*?)' . preg_quote($search_lower, '/') . '/s', $str_lower, $matches);
    if (isset($matches[1])) {
        return substr($str, strlen($matches[1]));
    }
    return FALSE;
}
示例#21
0
/**
 * UTF8::ucwords
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2011 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _ucwords($str)
{
    if (UTF8::is_ascii($str)) {
        return ucwords($str);
    }
    // [\x0c\x09\x0b\x0a\x0d\x20] matches form feeds, horizontal tabs, vertical tabs, linefeeds and carriage returns.
    // This corresponds to the definition of a 'word' defined at http://php.net/ucwords
    // Modified by Ivan Tcholakov, 27-DEC-2015.
    //return preg_replace(
    //	'/(?<=^|[\x0c\x09\x0b\x0a\x0d\x20])[^\x0c\x09\x0b\x0a\x0d\x20]/ue',
    //	'UTF8::strtoupper(\'$0\')',
    //	$str
    //);
    return preg_replace_callback('/(?<=^|[\\x0c\\x09\\x0b\\x0a\\x0d\\x20])[^\\x0c\\x09\\x0b\\x0a\\x0d\\x20]/u', '_ucwords_callback', $str);
    //
}
示例#22
0
/**
 * UTF8::strcspn
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2012 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _strcspn($str, $mask, $offset = NULL, $length = NULL)
{
    if ($str == '' or $mask == '') {
        return 0;
    }
    if (UTF8::is_ascii($str) and UTF8::is_ascii($mask)) {
        return $offset === NULL ? strcspn($str, $mask) : ($length === NULL ? strcspn($str, $mask, $offset) : strcspn($str, $mask, $offset, $length));
    }
    if ($offset !== NULL or $length !== NULL) {
        $str = UTF8::substr($str, $offset, $length);
    }
    // Escape these characters:  - [ ] . : \ ^ /
    // The . and : are escaped to prevent possible warnings about POSIX regex elements
    $mask = preg_replace('#[-[\\].:\\\\^/]#', '\\\\$0', $mask);
    preg_match('/^[^' . $mask . ']+/u', $str, $matches);
    return isset($matches[0]) ? UTF8::strlen($matches[0]) : 0;
}
示例#23
0
文件: strpos.php 项目: nevermlnd/cv
/**
 * UTF8::strpos
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2010 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _strpos($str, $search, $offset = 0)
{
	$offset = (int) $offset;

	if (UTF8::is_ascii($str) AND UTF8::is_ascii($search))
		return strpos($str, $search, $offset);

	if ($offset == 0)
	{
		$array = explode($search, $str, 2);
		return isset($array[1]) ? UTF8::strlen($array[0]) : FALSE;
	}

	$str = UTF8::substr($str, $offset);
	$pos = UTF8::strpos($str, $search);
	return ($pos === FALSE) ? FALSE : ($pos + $offset);
}
示例#24
0
文件: text.php 项目: anqh/core
 /**
  * Recursively cleans arrays, objects, and strings. Removes ASCII control
  * codes and converts to the requested charset while silently discarding
  * incompatible characters.
  *
  * @param   mixed  $str
  * @return  string
  */
 public static function clean($str)
 {
     if (is_array($str) || is_object($str)) {
         foreach ($str as $key => $val) {
             $str[$key] = self::clean($val);
         }
     } else {
         if (is_string($str) && $str !== '') {
             $str = mb_strtolower(UTF8::strip_ascii_ctrl($str));
             if (!UTF8::is_ascii($str)) {
                 $str = strtolower(self::transliterate_to_ascii($str));
             }
             if (!UTF8::is_ascii($str)) {
                 $str = UTF8::strip_non_ascii($str);
             }
         }
     }
     return $str;
 }
示例#25
0
 /**
  * Recursively cleans arrays, objects, and strings. Removes ASCII control
  * codes and converts to the requested charset while silently discarding
  * incompatible characters.
  *
  *     UTF8::clean($_GET); // Clean GET data
  *
  * @param   mixed   $var        variable to clean
  * @param   string  $charset    character set, defaults to Kohana::$charset
  * @return  mixed
  * @uses    UTF8::clean
  * @uses    UTF8::strip_ascii_ctrl
  * @uses    UTF8::is_ascii
  */
 public static function clean($var, $charset = 'utf-8')
 {
     if (is_array($var) or is_object($var)) {
         foreach ($var as $key => $val) {
             // Recursion!
             $var[UTF8::clean($key)] = UTF8::clean($val);
         }
     } elseif (is_string($var) and $var !== '') {
         // Remove control characters
         $var = UTF8::strip_ascii_ctrl($var);
         if (!UTF8::is_ascii($var)) {
             // Disable notices
             $error_reporting = error_reporting(~E_NOTICE);
             $var = mb_convert_encoding($var, $charset, $charset);
             // Turn notices back on
             error_reporting($error_reporting);
         }
     }
     return $var;
 }
示例#26
0
/**
 * UTF8::strtolower
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2011 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _strtolower($str)
{
    if (UTF8::is_ascii($str)) {
        return strtolower($str);
    }
    static $utf8_upper_to_lower = NULL;
    if ($utf8_upper_to_lower === NULL) {
        $utf8_upper_to_lower = array(0x41 => 0x61, 0x3a6 => 0x3c6, 0x162 => 0x163, 0xc5 => 0xe5, 0x42 => 0x62, 0x139 => 0x13a, 0xc1 => 0xe1, 0x141 => 0x142, 0x38e => 0x3cd, 0x100 => 0x101, 0x490 => 0x491, 0x394 => 0x3b4, 0x15a => 0x15b, 0x44 => 0x64, 0x393 => 0x3b3, 0xd4 => 0xf4, 0x42a => 0x44a, 0x419 => 0x439, 0x112 => 0x113, 0x41c => 0x43c, 0x15e => 0x15f, 0x143 => 0x144, 0xce => 0xee, 0x40e => 0x45e, 0x42f => 0x44f, 0x39a => 0x3ba, 0x154 => 0x155, 0x49 => 0x69, 0x53 => 0x73, 0x1e1e => 0x1e1f, 0x134 => 0x135, 0x427 => 0x447, 0x3a0 => 0x3c0, 0x418 => 0x438, 0xd3 => 0xf3, 0x420 => 0x440, 0x404 => 0x454, 0x415 => 0x435, 0x429 => 0x449, 0x14a => 0x14b, 0x411 => 0x431, 0x409 => 0x459, 0x1e02 => 0x1e03, 0xd6 => 0xf6, 0xd9 => 0xf9, 0x4e => 0x6e, 0x401 => 0x451, 0x3a4 => 0x3c4, 0x423 => 0x443, 0x15c => 0x15d, 0x403 => 0x453, 0x3a8 => 0x3c8, 0x158 => 0x159, 0x47 => 0x67, 0xc4 => 0xe4, 0x386 => 0x3ac, 0x389 => 0x3ae, 0x166 => 0x167, 0x39e => 0x3be, 0x164 => 0x165, 0x116 => 0x117, 0x108 => 0x109, 0x56 => 0x76, 0xde => 0xfe, 0x156 => 0x157, 0xda => 0xfa, 0x1e60 => 0x1e61, 0x1e82 => 0x1e83, 0xc2 => 0xe2, 0x118 => 0x119, 0x145 => 0x146, 0x50 => 0x70, 0x150 => 0x151, 0x42e => 0x44e, 0x128 => 0x129, 0x3a7 => 0x3c7, 0x13d => 0x13e, 0x422 => 0x442, 0x5a => 0x7a, 0x428 => 0x448, 0x3a1 => 0x3c1, 0x1e80 => 0x1e81, 0x16c => 0x16d, 0xd5 => 0xf5, 0x55 => 0x75, 0x176 => 0x177, 0xdc => 0xfc, 0x1e56 => 0x1e57, 0x3a3 => 0x3c3, 0x41a => 0x43a, 0x4d => 0x6d, 0x16a => 0x16b, 0x170 => 0x171, 0x424 => 0x444, 0xcc => 0xec, 0x168 => 0x169, 0x39f => 0x3bf, 0x4b => 0x6b, 0xd2 => 0xf2, 0xc0 => 0xe0, 0x414 => 0x434, 0x3a9 => 0x3c9, 0x1e6a => 0x1e6b, 0xc3 => 0xe3, 0x42d => 0x44d, 0x416 => 0x436, 0x1a0 => 0x1a1, 0x10c => 0x10d, 0x11c => 0x11d, 0xd0 => 0xf0, 0x13b => 0x13c, 0x40f => 0x45f, 0x40a => 0x45a, 0xc8 => 0xe8, 0x3a5 => 0x3c5, 0x46 => 0x66, 0xdd => 0xfd, 0x43 => 0x63, 0x21a => 0x21b, 0xca => 0xea, 0x399 => 0x3b9, 0x179 => 0x17a, 0xcf => 0xef, 0x1af => 0x1b0, 0x45 => 0x65, 0x39b => 0x3bb, 0x398 => 0x3b8, 0x39c => 0x3bc, 0x40c => 0x45c, 0x41f => 0x43f, 0x42c => 0x44c, 0xde => 0xfe, 0xd0 => 0xf0, 0x1ef2 => 0x1ef3, 0x48 => 0x68, 0xcb => 0xeb, 0x110 => 0x111, 0x413 => 0x433, 0x12e => 0x12f, 0xc6 => 0xe6, 0x58 => 0x78, 0x160 => 0x161, 0x16e => 0x16f, 0x391 => 0x3b1, 0x407 => 0x457, 0x172 => 0x173, 0x178 => 0xff, 0x4f => 0x6f, 0x41b => 0x43b, 0x395 => 0x3b5, 0x425 => 0x445, 0x120 => 0x121, 0x17d => 0x17e, 0x17b => 0x17c, 0x396 => 0x3b6, 0x392 => 0x3b2, 0x388 => 0x3ad, 0x1e84 => 0x1e85, 0x174 => 0x175, 0x51 => 0x71, 0x417 => 0x437, 0x1e0a => 0x1e0b, 0x147 => 0x148, 0x104 => 0x105, 0x408 => 0x458, 0x14c => 0x14d, 0xcd => 0xed, 0x59 => 0x79, 0x10a => 0x10b, 0x38f => 0x3ce, 0x52 => 0x72, 0x410 => 0x430, 0x405 => 0x455, 0x402 => 0x452, 0x126 => 0x127, 0x136 => 0x137, 0x12a => 0x12b, 0x38a => 0x3af, 0x42b => 0x44b, 0x4c => 0x6c, 0x397 => 0x3b7, 0x124 => 0x125, 0x218 => 0x219, 0xdb => 0xfb, 0x11e => 0x11f, 0x41e => 0x43e, 0x1e40 => 0x1e41, 0x39d => 0x3bd, 0x106 => 0x107, 0x3ab => 0x3cb, 0x426 => 0x446, 0xde => 0xfe, 0xc7 => 0xe7, 0x3aa => 0x3ca, 0x421 => 0x441, 0x412 => 0x432, 0x10e => 0x10f, 0xd8 => 0xf8, 0x57 => 0x77, 0x11a => 0x11b, 0x54 => 0x74, 0x4a => 0x6a, 0x40b => 0x45b, 0x406 => 0x456, 0x102 => 0x103, 0x39b => 0x3bb, 0xd1 => 0xf1, 0x41d => 0x43d, 0x38c => 0x3cc, 0xc9 => 0xe9, 0xd0 => 0xf0, 0x407 => 0x457, 0x122 => 0x123);
    }
    $uni = UTF8::to_unicode($str);
    if ($uni === FALSE) {
        return FALSE;
    }
    for ($i = 0, $c = count($uni); $i < $c; $i++) {
        if (isset($utf8_upper_to_lower[$uni[$i]])) {
            $uni[$i] = $utf8_upper_to_lower[$uni[$i]];
        }
    }
    return UTF8::from_unicode($uni);
}
示例#27
0
/**
 * UTF8::strtoupper
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2011 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _strtoupper($str)
{
    if (UTF8::is_ascii($str)) {
        return strtoupper($str);
    }
    static $utf8_lower_to_upper = NULL;
    if ($utf8_lower_to_upper === NULL) {
        $utf8_lower_to_upper = array(0x61 => 0x41, 0x3c6 => 0x3a6, 0x163 => 0x162, 0xe5 => 0xc5, 0x62 => 0x42, 0x13a => 0x139, 0xe1 => 0xc1, 0x142 => 0x141, 0x3cd => 0x38e, 0x101 => 0x100, 0x491 => 0x490, 0x3b4 => 0x394, 0x15b => 0x15a, 0x64 => 0x44, 0x3b3 => 0x393, 0xf4 => 0xd4, 0x44a => 0x42a, 0x439 => 0x419, 0x113 => 0x112, 0x43c => 0x41c, 0x15f => 0x15e, 0x144 => 0x143, 0xee => 0xce, 0x45e => 0x40e, 0x44f => 0x42f, 0x3ba => 0x39a, 0x155 => 0x154, 0x69 => 0x49, 0x73 => 0x53, 0x1e1f => 0x1e1e, 0x135 => 0x134, 0x447 => 0x427, 0x3c0 => 0x3a0, 0x438 => 0x418, 0xf3 => 0xd3, 0x440 => 0x420, 0x454 => 0x404, 0x435 => 0x415, 0x449 => 0x429, 0x14b => 0x14a, 0x431 => 0x411, 0x459 => 0x409, 0x1e03 => 0x1e02, 0xf6 => 0xd6, 0xf9 => 0xd9, 0x6e => 0x4e, 0x451 => 0x401, 0x3c4 => 0x3a4, 0x443 => 0x423, 0x15d => 0x15c, 0x453 => 0x403, 0x3c8 => 0x3a8, 0x159 => 0x158, 0x67 => 0x47, 0xe4 => 0xc4, 0x3ac => 0x386, 0x3ae => 0x389, 0x167 => 0x166, 0x3be => 0x39e, 0x165 => 0x164, 0x117 => 0x116, 0x109 => 0x108, 0x76 => 0x56, 0xfe => 0xde, 0x157 => 0x156, 0xfa => 0xda, 0x1e61 => 0x1e60, 0x1e83 => 0x1e82, 0xe2 => 0xc2, 0x119 => 0x118, 0x146 => 0x145, 0x70 => 0x50, 0x151 => 0x150, 0x44e => 0x42e, 0x129 => 0x128, 0x3c7 => 0x3a7, 0x13e => 0x13d, 0x442 => 0x422, 0x7a => 0x5a, 0x448 => 0x428, 0x3c1 => 0x3a1, 0x1e81 => 0x1e80, 0x16d => 0x16c, 0xf5 => 0xd5, 0x75 => 0x55, 0x177 => 0x176, 0xfc => 0xdc, 0x1e57 => 0x1e56, 0x3c3 => 0x3a3, 0x43a => 0x41a, 0x6d => 0x4d, 0x16b => 0x16a, 0x171 => 0x170, 0x444 => 0x424, 0xec => 0xcc, 0x169 => 0x168, 0x3bf => 0x39f, 0x6b => 0x4b, 0xf2 => 0xd2, 0xe0 => 0xc0, 0x434 => 0x414, 0x3c9 => 0x3a9, 0x1e6b => 0x1e6a, 0xe3 => 0xc3, 0x44d => 0x42d, 0x436 => 0x416, 0x1a1 => 0x1a0, 0x10d => 0x10c, 0x11d => 0x11c, 0xf0 => 0xd0, 0x13c => 0x13b, 0x45f => 0x40f, 0x45a => 0x40a, 0xe8 => 0xc8, 0x3c5 => 0x3a5, 0x66 => 0x46, 0xfd => 0xdd, 0x63 => 0x43, 0x21b => 0x21a, 0xea => 0xca, 0x3b9 => 0x399, 0x17a => 0x179, 0xef => 0xcf, 0x1b0 => 0x1af, 0x65 => 0x45, 0x3bb => 0x39b, 0x3b8 => 0x398, 0x3bc => 0x39c, 0x45c => 0x40c, 0x43f => 0x41f, 0x44c => 0x42c, 0xfe => 0xde, 0xf0 => 0xd0, 0x1ef3 => 0x1ef2, 0x68 => 0x48, 0xeb => 0xcb, 0x111 => 0x110, 0x433 => 0x413, 0x12f => 0x12e, 0xe6 => 0xc6, 0x78 => 0x58, 0x161 => 0x160, 0x16f => 0x16e, 0x3b1 => 0x391, 0x457 => 0x407, 0x173 => 0x172, 0xff => 0x178, 0x6f => 0x4f, 0x43b => 0x41b, 0x3b5 => 0x395, 0x445 => 0x425, 0x121 => 0x120, 0x17e => 0x17d, 0x17c => 0x17b, 0x3b6 => 0x396, 0x3b2 => 0x392, 0x3ad => 0x388, 0x1e85 => 0x1e84, 0x175 => 0x174, 0x71 => 0x51, 0x437 => 0x417, 0x1e0b => 0x1e0a, 0x148 => 0x147, 0x105 => 0x104, 0x458 => 0x408, 0x14d => 0x14c, 0xed => 0xcd, 0x79 => 0x59, 0x10b => 0x10a, 0x3ce => 0x38f, 0x72 => 0x52, 0x430 => 0x410, 0x455 => 0x405, 0x452 => 0x402, 0x127 => 0x126, 0x137 => 0x136, 0x12b => 0x12a, 0x3af => 0x38a, 0x44b => 0x42b, 0x6c => 0x4c, 0x3b7 => 0x397, 0x125 => 0x124, 0x219 => 0x218, 0xfb => 0xdb, 0x11f => 0x11e, 0x43e => 0x41e, 0x1e41 => 0x1e40, 0x3bd => 0x39d, 0x107 => 0x106, 0x3cb => 0x3ab, 0x446 => 0x426, 0xfe => 0xde, 0xe7 => 0xc7, 0x3ca => 0x3aa, 0x441 => 0x421, 0x432 => 0x412, 0x10f => 0x10e, 0xf8 => 0xd8, 0x77 => 0x57, 0x11b => 0x11a, 0x74 => 0x54, 0x6a => 0x4a, 0x45b => 0x40b, 0x456 => 0x406, 0x103 => 0x102, 0x3bb => 0x39b, 0xf1 => 0xd1, 0x43d => 0x41d, 0x3cc => 0x38c, 0xe9 => 0xc9, 0xf0 => 0xd0, 0x457 => 0x407, 0x123 => 0x122);
    }
    $uni = UTF8::to_unicode($str);
    if ($uni === FALSE) {
        return FALSE;
    }
    for ($i = 0, $c = count($uni); $i < $c; $i++) {
        if (isset($utf8_lower_to_upper[$uni[$i]])) {
            $uni[$i] = $utf8_lower_to_upper[$uni[$i]];
        }
    }
    return UTF8::from_unicode($uni);
}
示例#28
0
文件: UTF8Test.php 项目: azuya/Wi3
 /**
  * Tests UTF8::is_ascii
  *
  * @test
  * @dataProvider provider_is_ascii
  */
 public function test_is_ascii($input, $expected)
 {
     $this->assertSame($expected, UTF8::is_ascii($input));
 }
示例#29
0
/**
 * UTF8::strtolower
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2010 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _strtolower($str)
{
	if (UTF8::is_ascii($str))
		return strtolower($str);

	static $utf8_upper_to_lower = NULL;

	if ($utf8_upper_to_lower === NULL)
	{
		$utf8_upper_to_lower = array(
			0x0041=>0x0061, 0x03A6=>0x03C6, 0x0162=>0x0163, 0x00C5=>0x00E5, 0x0042=>0x0062,
			0x0139=>0x013A, 0x00C1=>0x00E1, 0x0141=>0x0142, 0x038E=>0x03CD, 0x0100=>0x0101,
			0x0490=>0x0491, 0x0394=>0x03B4, 0x015A=>0x015B, 0x0044=>0x0064, 0x0393=>0x03B3,
			0x00D4=>0x00F4, 0x042A=>0x044A, 0x0419=>0x0439, 0x0112=>0x0113, 0x041C=>0x043C,
			0x015E=>0x015F, 0x0143=>0x0144, 0x00CE=>0x00EE, 0x040E=>0x045E, 0x042F=>0x044F,
			0x039A=>0x03BA, 0x0154=>0x0155, 0x0049=>0x0069, 0x0053=>0x0073, 0x1E1E=>0x1E1F,
			0x0134=>0x0135, 0x0427=>0x0447, 0x03A0=>0x03C0, 0x0418=>0x0438, 0x00D3=>0x00F3,
			0x0420=>0x0440, 0x0404=>0x0454, 0x0415=>0x0435, 0x0429=>0x0449, 0x014A=>0x014B,
			0x0411=>0x0431, 0x0409=>0x0459, 0x1E02=>0x1E03, 0x00D6=>0x00F6, 0x00D9=>0x00F9,
			0x004E=>0x006E, 0x0401=>0x0451, 0x03A4=>0x03C4, 0x0423=>0x0443, 0x015C=>0x015D,
			0x0403=>0x0453, 0x03A8=>0x03C8, 0x0158=>0x0159, 0x0047=>0x0067, 0x00C4=>0x00E4,
			0x0386=>0x03AC, 0x0389=>0x03AE, 0x0166=>0x0167, 0x039E=>0x03BE, 0x0164=>0x0165,
			0x0116=>0x0117, 0x0108=>0x0109, 0x0056=>0x0076, 0x00DE=>0x00FE, 0x0156=>0x0157,
			0x00DA=>0x00FA, 0x1E60=>0x1E61, 0x1E82=>0x1E83, 0x00C2=>0x00E2, 0x0118=>0x0119,
			0x0145=>0x0146, 0x0050=>0x0070, 0x0150=>0x0151, 0x042E=>0x044E, 0x0128=>0x0129,
			0x03A7=>0x03C7, 0x013D=>0x013E, 0x0422=>0x0442, 0x005A=>0x007A, 0x0428=>0x0448,
			0x03A1=>0x03C1, 0x1E80=>0x1E81, 0x016C=>0x016D, 0x00D5=>0x00F5, 0x0055=>0x0075,
			0x0176=>0x0177, 0x00DC=>0x00FC, 0x1E56=>0x1E57, 0x03A3=>0x03C3, 0x041A=>0x043A,
			0x004D=>0x006D, 0x016A=>0x016B, 0x0170=>0x0171, 0x0424=>0x0444, 0x00CC=>0x00EC,
			0x0168=>0x0169, 0x039F=>0x03BF, 0x004B=>0x006B, 0x00D2=>0x00F2, 0x00C0=>0x00E0,
			0x0414=>0x0434, 0x03A9=>0x03C9, 0x1E6A=>0x1E6B, 0x00C3=>0x00E3, 0x042D=>0x044D,
			0x0416=>0x0436, 0x01A0=>0x01A1, 0x010C=>0x010D, 0x011C=>0x011D, 0x00D0=>0x00F0,
			0x013B=>0x013C, 0x040F=>0x045F, 0x040A=>0x045A, 0x00C8=>0x00E8, 0x03A5=>0x03C5,
			0x0046=>0x0066, 0x00DD=>0x00FD, 0x0043=>0x0063, 0x021A=>0x021B, 0x00CA=>0x00EA,
			0x0399=>0x03B9, 0x0179=>0x017A, 0x00CF=>0x00EF, 0x01AF=>0x01B0, 0x0045=>0x0065,
			0x039B=>0x03BB, 0x0398=>0x03B8, 0x039C=>0x03BC, 0x040C=>0x045C, 0x041F=>0x043F,
			0x042C=>0x044C, 0x00DE=>0x00FE, 0x00D0=>0x00F0, 0x1EF2=>0x1EF3, 0x0048=>0x0068,
			0x00CB=>0x00EB, 0x0110=>0x0111, 0x0413=>0x0433, 0x012E=>0x012F, 0x00C6=>0x00E6,
			0x0058=>0x0078, 0x0160=>0x0161, 0x016E=>0x016F, 0x0391=>0x03B1, 0x0407=>0x0457,
			0x0172=>0x0173, 0x0178=>0x00FF, 0x004F=>0x006F, 0x041B=>0x043B, 0x0395=>0x03B5,
			0x0425=>0x0445, 0x0120=>0x0121, 0x017D=>0x017E, 0x017B=>0x017C, 0x0396=>0x03B6,
			0x0392=>0x03B2, 0x0388=>0x03AD, 0x1E84=>0x1E85, 0x0174=>0x0175, 0x0051=>0x0071,
			0x0417=>0x0437, 0x1E0A=>0x1E0B, 0x0147=>0x0148, 0x0104=>0x0105, 0x0408=>0x0458,
			0x014C=>0x014D, 0x00CD=>0x00ED, 0x0059=>0x0079, 0x010A=>0x010B, 0x038F=>0x03CE,
			0x0052=>0x0072, 0x0410=>0x0430, 0x0405=>0x0455, 0x0402=>0x0452, 0x0126=>0x0127,
			0x0136=>0x0137, 0x012A=>0x012B, 0x038A=>0x03AF, 0x042B=>0x044B, 0x004C=>0x006C,
			0x0397=>0x03B7, 0x0124=>0x0125, 0x0218=>0x0219, 0x00DB=>0x00FB, 0x011E=>0x011F,
			0x041E=>0x043E, 0x1E40=>0x1E41, 0x039D=>0x03BD, 0x0106=>0x0107, 0x03AB=>0x03CB,
			0x0426=>0x0446, 0x00DE=>0x00FE, 0x00C7=>0x00E7, 0x03AA=>0x03CA, 0x0421=>0x0441,
			0x0412=>0x0432, 0x010E=>0x010F, 0x00D8=>0x00F8, 0x0057=>0x0077, 0x011A=>0x011B,
			0x0054=>0x0074, 0x004A=>0x006A, 0x040B=>0x045B, 0x0406=>0x0456, 0x0102=>0x0103,
			0x039B=>0x03BB, 0x00D1=>0x00F1, 0x041D=>0x043D, 0x038C=>0x03CC, 0x00C9=>0x00E9,
			0x00D0=>0x00F0, 0x0407=>0x0457, 0x0122=>0x0123,
		);
	}

	$uni = UTF8::to_unicode($str);

	if ($uni === FALSE)
		return FALSE;

	for ($i = 0, $c = count($uni); $i < $c; $i++)
	{
		if (isset($utf8_upper_to_lower[$uni[$i]]))
		{
			$uni[$i] = $utf8_upper_to_lower[$uni[$i]];
		}
	}

	return UTF8::from_unicode($uni);
}
示例#30
0
文件: text.php 项目: Normull/core
 /**
  * Generates a random string of a given type and length.
  *
  * @param   string   a type of pool, or a string of characters to use as the pool
  * @param   integer  length of string to return
  * @return  string
  *
  * @tutorial  alnum     alpha-numeric characters
  * @tutorial  alpha     alphabetical characters
  * @tutorial  hexdec    hexadecimal characters, 0-9 plus a-f
  * @tutorial  numeric   digit characters, 0-9
  * @tutorial  nozero    digit characters, 1-9
  * @tutorial  distinct  clearly distinct alpha-numeric characters
  */
 public static function random($type = 'alnum', $length = 8)
 {
     $utf8 = FALSE;
     switch ($type) {
         case 'alnum':
             $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
             break;
         case 'alpha':
             $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
             break;
         case 'hexdec':
             $pool = '0123456789abcdef';
             break;
         case 'numeric':
             $pool = '0123456789';
             break;
         case 'nozero':
             $pool = '123456789';
             break;
         case 'distinct':
             $pool = '2345679ACDEFHJKLMNPRSTUVWXYZ';
             break;
         default:
             $pool = (string) $type;
             $utf8 = !UTF8::is_ascii($pool);
             break;
     }
     // Split the pool into an array of characters
     $pool = $utf8 === TRUE ? UTF8::str_split($pool, 1) : str_split($pool, 1);
     // Largest pool key
     $max = count($pool) - 1;
     $str = '';
     for ($i = 0; $i < $length; $i++) {
         // Select a random character from the pool and add it to the string
         $str .= $pool[mt_rand(0, $max)];
     }
     // Make sure alnum strings contain at least one letter and one digit
     if ($type === 'alnum' and $length > 1) {
         if (ctype_alpha($str)) {
             // Add a random digit
             $str[mt_rand(0, $length - 1)] = chr(mt_rand(48, 57));
         } elseif (ctype_digit($str)) {
             // Add a random letter
             $str[mt_rand(0, $length - 1)] = chr(mt_rand(65, 90));
         }
     }
     return $str;
 }