refactoring string class.
This commit is contained in:
parent
442904b277
commit
3c05f7260c
|
@ -63,3 +63,8 @@ function __($key, $replacements = array(), $language = null)
|
|||
{
|
||||
return Lang::line($key, $replacements, $language);
|
||||
}
|
||||
|
||||
function fe($function)
|
||||
{
|
||||
return function_exists($function);
|
||||
}
|
|
@ -5,78 +5,93 @@ class Str {
|
|||
/**
|
||||
* 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
|
||||
* @return string
|
||||
*/
|
||||
public static function lower($value)
|
||||
{
|
||||
if (function_exists('mb_strtolower'))
|
||||
{
|
||||
return mb_strtolower($value, Config::get('application.encoding'));
|
||||
}
|
||||
|
||||
return strtolower($value);
|
||||
return (fe('mb_strtolower')) ? mb_strtolower($value, Config::get('application.encoding')) : strtolower($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return string
|
||||
*/
|
||||
public static function upper($value)
|
||||
{
|
||||
if (function_exists('mb_strtoupper'))
|
||||
{
|
||||
return mb_strtoupper($value, Config::get('application.encoding'));
|
||||
}
|
||||
|
||||
return strtoupper($value);
|
||||
return (fe('mb_strtoupper')) ? mb_strtoupper($value, Config::get('application.encoding')) : 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
|
||||
* @return string
|
||||
*/
|
||||
public static function title($value)
|
||||
{
|
||||
if (function_exists('mb_convert_case'))
|
||||
{
|
||||
return mb_convert_case($value, MB_CASE_TITLE, Config::get('application.encoding'));
|
||||
}
|
||||
|
||||
return ucwords(strtolower($value));
|
||||
return (fe('mb_convert_case')) ? mb_convert_case($value, MB_CASE_TITLE, Config::get('application.encoding')) : ucwords(strtolower($value));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return int
|
||||
*/
|
||||
public static function length($value)
|
||||
{
|
||||
if (function_exists('mb_strlen'))
|
||||
{
|
||||
return mb_strlen($value, Config::get('application.encoding'));
|
||||
}
|
||||
|
||||
return strlen($value);
|
||||
return (fe('mb_strlen')) ? mb_strlen($value, Config::get('application.encoding')) : strlen($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a string to 7-bit ASCII.
|
||||
*
|
||||
* <code>
|
||||
* // Returns "Deuxieme Article"
|
||||
* echo Str::ascii('Deuxième Article');
|
||||
* </code>
|
||||
*
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
public static function ascii($value)
|
||||
{
|
||||
$foreign = Config::get('ascii');
|
||||
|
||||
$value = preg_replace(array_keys($foreign), array_values($foreign), $value);
|
||||
$value = preg_replace(array_keys($foreign = Config::get('ascii')), array_values($foreign), $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.
|
||||
*
|
||||
* 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 string $type
|
||||
|
|
Loading…
Reference in New Issue