Files
spa-api/system/str.php

116 lines
2.8 KiB
PHP
Raw Normal View History

2011-06-08 23:45:08 -05:00
<?php namespace System;
class Str {
/**
* Convert HTML characters to entities.
*
* @param string $value
* @return string
*/
public static function entities($value)
{
return htmlentities($value, ENT_QUOTES, Config::get('application.encoding'), false);
2011-06-08 23:45:08 -05:00
}
/**
* Convert a string to lowercase.
*
* @param string $value
* @return string
*/
public static function lower($value)
{
return function_exists('mb_strtolower') ? mb_strtolower($value, Config::get('application.encoding')) : strtolower($value);
2011-06-08 23:45:08 -05:00
}
/**
* Convert a string to uppercase.
*
* @param string $value
* @return string
*/
public static function upper($value)
{
return function_exists('mb_strtoupper') ? mb_strtoupper($value, Config::get('application.encoding')) : strtoupper($value);
2011-06-08 23:45:08 -05:00
}
/**
* Convert a string to title case (ucwords).
*
* @param string $value
* @return string
*/
public static function title($value)
{
return (function_exists('mb_convert_case')) ? mb_convert_case($value, MB_CASE_TITLE, Config::get('application.encoding')) : ucwords(strtolower($value));
2011-06-08 23:45:08 -05:00
}
2011-06-21 20:11:17 -05:00
/**
* Get the length of a string.
*
* @param string $value
* @return int
*/
public static function length($value)
{
return function_exists('mb_strlen') ? mb_strlen($value, Config::get('application.encoding')) : strlen($value);
}
/**
* Convert a string to 7-bit ASCII.
*
* @param string $value
* @return string
*/
public static function ascii($value)
{
$foreign = Config::get('ascii');
$value = preg_replace(array_keys($foreign), array_values($foreign), $value);
return preg_replace('/[^\x09\x0A\x0D\x20-\x7E]/', '', $value);
}
2011-06-08 23:45:08 -05:00
/**
* Generate a random alpha or alpha-numeric string.
*
2011-07-12 20:04:14 -05:00
* Supported types: 'alpha_num' and 'alpha'.
2011-06-08 23:45:08 -05:00
*
* @param int $length
* @param string $type
2011-06-08 23:45:08 -05:00
* @return string
*/
2011-07-12 20:04:14 -05:00
public static function random($length = 16, $type = 'alpha_num')
2011-06-08 23:45:08 -05:00
{
$value = '';
2011-07-07 07:14:57 -07:00
$pool_length = strlen($pool = static::pool($type)) - 1;
2011-06-08 23:45:08 -05:00
for ($i = 0; $i < $length; $i++)
{
$value .= $pool[mt_rand(0, $pool_length)];
2011-06-08 23:45:08 -05:00
}
return $value;
}
2011-07-07 07:14:57 -07:00
/**
* Get a chracter pool.
*
* @param string $type
* @return string
*/
2011-07-12 20:04:14 -05:00
private static function pool($type = 'alpha_num')
2011-07-07 07:14:57 -07:00
{
2011-07-07 07:16:20 -07:00
switch ($type)
2011-07-07 07:14:57 -07:00
{
2011-07-12 20:04:14 -05:00
case 'alpha_num':
2011-07-07 07:16:20 -07:00
return '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
default:
return 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
2011-07-07 07:14:57 -07:00
}
}
2011-06-08 23:45:08 -05:00
}