Files
spa-api/laravel/str.php

121 lines
2.8 KiB
PHP
Raw Normal View History

2011-08-18 19:56:29 -05:00
<?php namespace Laravel;
2011-06-08 23:45:08 -05:00
class Str {
2011-08-08 14:31:12 -05:00
/**
* Convert a string to lowercase.
*
2011-09-28 22:25:54 -05:00
* <code>
* // Convert a string to lowercase
* echo Str::lower('STOP YELLING');
*
* // Convert a UTF-8 string to lowercase
* echo Str::lower('Τάχιστη');
* </code>
*
2011-08-08 14:31:12 -05:00
* @param string $value
* @return string
*/
public static function lower($value)
{
2011-09-28 22:25:54 -05:00
return (fe('mb_strtolower')) ? mb_strtolower($value, Config::get('application.encoding')) : strtolower($value);
2011-08-08 14:31:12 -05:00
}
2011-06-08 23:45:08 -05:00
2011-08-08 14:31:12 -05:00
/**
* Convert a string to uppercase.
*
2011-09-28 22:25:54 -05:00
* <code>
* // Convert a string to uppercase
* echo Str::upper('speak louder');
*
* // Convert a UTF-8 string to uppercase
* echo Str::upper('Τάχιστη');
* </code>
*
2011-08-08 14:31:12 -05:00
* @param string $value
* @return string
*/
public static function upper($value)
{
2011-09-28 22:25:54 -05:00
return (fe('mb_strtoupper')) ? mb_strtoupper($value, Config::get('application.encoding')) : strtoupper($value);
2011-08-08 14:31:12 -05:00
}
2011-06-08 23:45:08 -05:00
2011-08-08 14:31:12 -05:00
/**
2011-09-28 22:25:54 -05:00
* 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>
2011-08-08 14:31:12 -05:00
*
* @param string $value
* @return string
*/
public static function title($value)
{
2011-09-28 22:25:54 -05:00
return (fe('mb_convert_case')) ? mb_convert_case($value, MB_CASE_TITLE, Config::get('application.encoding')) : ucwords(strtolower($value));
2011-08-08 14:31:12 -05:00
}
2011-06-08 23:45:08 -05:00
2011-08-08 14:31:12 -05:00
/**
* Get the length of a string.
*
2011-09-28 22:25:54 -05:00
* <code>
* // Get the length of a string
* echo Str::length('taylor otwell');
*
* // Get the length of a UTF-8 string
* echo Str::length('Τάχιστη αλώπηξ');
* </code>
*
2011-08-08 14:31:12 -05:00
* @param string $value
* @return int
*/
public static function length($value)
{
2011-09-28 22:25:54 -05:00
return (fe('mb_strlen')) ? mb_strlen($value, Config::get('application.encoding')) : strlen($value);
2011-08-08 14:31:12 -05:00
}
2011-06-21 20:11:17 -05:00
2011-08-08 14:31:12 -05:00
/**
* Convert a string to 7-bit ASCII.
*
2011-09-28 22:25:54 -05:00
* <code>
* // Returns "Deuxieme Article"
* echo Str::ascii('Deuxième Article');
* </code>
*
2011-08-08 14:31:12 -05:00
* @param string $value
* @return string
*/
public static function ascii($value)
{
2011-09-28 22:25:54 -05:00
$value = preg_replace(array_keys($foreign = Config::get('ascii')), array_values($foreign), $value);
2011-08-08 14:31:12 -05:00
return preg_replace('/[^\x09\x0A\x0D\x20-\x7E]/', '', $value);
}
2011-08-08 14:31:12 -05:00
/**
* Generate a random alpha or alpha-numeric string.
*
2011-09-28 22:25:54 -05:00
* <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>
2011-08-08 14:31:12 -05:00
*
2011-09-28 22:12:41 -05:00
* @param int $length
2011-08-08 14:31:12 -05:00
* @param string $type
* @return string
*/
2011-09-28 22:12:41 -05:00
public static function random($length, $type = 'alnum')
2011-08-08 14:31:12 -05:00
{
2011-09-28 22:12:41 -05:00
$pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
2011-06-08 23:45:08 -05:00
2011-09-28 22:12:41 -05:00
return substr(str_shuffle(str_repeat(($type == 'alnum') ? $pool.'0123456789' : $pool, 5)), 0, $length);
2011-08-08 14:31:12 -05:00
}
2011-07-07 07:14:57 -07:00
2011-06-08 23:45:08 -05:00
}