refactoring string class.
This commit is contained in:
parent
442904b277
commit
3c05f7260c
|
@ -62,4 +62,9 @@ function e($value)
|
||||||
function __($key, $replacements = array(), $language = null)
|
function __($key, $replacements = array(), $language = null)
|
||||||
{
|
{
|
||||||
return Lang::line($key, $replacements, $language);
|
return Lang::line($key, $replacements, $language);
|
||||||
|
}
|
||||||
|
|
||||||
|
function fe($function)
|
||||||
|
{
|
||||||
|
return function_exists($function);
|
||||||
}
|
}
|
|
@ -5,78 +5,93 @@ class Str {
|
||||||
/**
|
/**
|
||||||
* Convert a string to lowercase.
|
* Convert a string to lowercase.
|
||||||
*
|
*
|
||||||
|
* <code>
|
||||||
|
* // Convert a string to lowercase
|
||||||
|
* echo Str::lower('STOP YELLING');
|
||||||
|
*
|
||||||
|
* // Convert a UTF-8 string to lowercase
|
||||||
|
* echo Str::lower('Τάχιστη');
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
* @param string $value
|
* @param string $value
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function lower($value)
|
public static function lower($value)
|
||||||
{
|
{
|
||||||
if (function_exists('mb_strtolower'))
|
return (fe('mb_strtolower')) ? mb_strtolower($value, Config::get('application.encoding')) : strtolower($value);
|
||||||
{
|
|
||||||
return mb_strtolower($value, Config::get('application.encoding'));
|
|
||||||
}
|
|
||||||
|
|
||||||
return strtolower($value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a string to uppercase.
|
* Convert a string to uppercase.
|
||||||
*
|
*
|
||||||
|
* <code>
|
||||||
|
* // Convert a string to uppercase
|
||||||
|
* echo Str::upper('speak louder');
|
||||||
|
*
|
||||||
|
* // Convert a UTF-8 string to uppercase
|
||||||
|
* echo Str::upper('Τάχιστη');
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
* @param string $value
|
* @param string $value
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function upper($value)
|
public static function upper($value)
|
||||||
{
|
{
|
||||||
if (function_exists('mb_strtoupper'))
|
return (fe('mb_strtoupper')) ? mb_strtoupper($value, Config::get('application.encoding')) : strtoupper($value);
|
||||||
{
|
|
||||||
return mb_strtoupper($value, Config::get('application.encoding'));
|
|
||||||
}
|
|
||||||
|
|
||||||
return strtoupper($value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a string to title case (ucwords).
|
* Convert a string to title case (ucwords equivalent).
|
||||||
|
*
|
||||||
|
* <code>
|
||||||
|
* // Convert a string to title case
|
||||||
|
* echo Str::title('taylor otwell');
|
||||||
|
*
|
||||||
|
* // Convert a UTF-8 string to title case
|
||||||
|
* echo Str::title('Τάχιστη αλώπηξ');
|
||||||
|
* </code>
|
||||||
*
|
*
|
||||||
* @param string $value
|
* @param string $value
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function title($value)
|
public static function title($value)
|
||||||
{
|
{
|
||||||
if (function_exists('mb_convert_case'))
|
return (fe('mb_convert_case')) ? mb_convert_case($value, MB_CASE_TITLE, Config::get('application.encoding')) : ucwords(strtolower($value));
|
||||||
{
|
|
||||||
return mb_convert_case($value, MB_CASE_TITLE, Config::get('application.encoding'));
|
|
||||||
}
|
|
||||||
|
|
||||||
return ucwords(strtolower($value));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the length of a string.
|
* Get the length of a string.
|
||||||
*
|
*
|
||||||
|
* <code>
|
||||||
|
* // Get the length of a string
|
||||||
|
* echo Str::length('taylor otwell');
|
||||||
|
*
|
||||||
|
* // Get the length of a UTF-8 string
|
||||||
|
* echo Str::length('Τάχιστη αλώπηξ');
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
* @param string $value
|
* @param string $value
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public static function length($value)
|
public static function length($value)
|
||||||
{
|
{
|
||||||
if (function_exists('mb_strlen'))
|
return (fe('mb_strlen')) ? mb_strlen($value, Config::get('application.encoding')) : strlen($value);
|
||||||
{
|
|
||||||
return mb_strlen($value, Config::get('application.encoding'));
|
|
||||||
}
|
|
||||||
|
|
||||||
return strlen($value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a string to 7-bit ASCII.
|
* Convert a string to 7-bit ASCII.
|
||||||
*
|
*
|
||||||
|
* <code>
|
||||||
|
* // Returns "Deuxieme Article"
|
||||||
|
* echo Str::ascii('Deuxième Article');
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
* @param string $value
|
* @param string $value
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function ascii($value)
|
public static function ascii($value)
|
||||||
{
|
{
|
||||||
$foreign = Config::get('ascii');
|
$value = preg_replace(array_keys($foreign = Config::get('ascii')), array_values($foreign), $value);
|
||||||
|
|
||||||
$value = preg_replace(array_keys($foreign), array_values($foreign), $value);
|
|
||||||
|
|
||||||
return preg_replace('/[^\x09\x0A\x0D\x20-\x7E]/', '', $value);
|
return preg_replace('/[^\x09\x0A\x0D\x20-\x7E]/', '', $value);
|
||||||
}
|
}
|
||||||
|
@ -84,7 +99,13 @@ public static function ascii($value)
|
||||||
/**
|
/**
|
||||||
* Generate a random alpha or alpha-numeric string.
|
* Generate a random alpha or alpha-numeric string.
|
||||||
*
|
*
|
||||||
* Supported types: 'alnum' and 'alpha'.
|
* <code>
|
||||||
|
* // Generate a 40 character random, alpha-numeric string
|
||||||
|
* echo Str::random(40);
|
||||||
|
*
|
||||||
|
* // Generate a 16 character random, alphabetic string
|
||||||
|
* echo Str::random(16, 'alpha');
|
||||||
|
* <code>
|
||||||
*
|
*
|
||||||
* @param int $length
|
* @param int $length
|
||||||
* @param string $type
|
* @param string $type
|
||||||
|
|
Loading…
Reference in New Issue