Files
spa-api/laravel/str.php

112 lines
2.1 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.
*
* @param string $value
* @return string
*/
public static function lower($value)
{
2011-09-08 17:49:16 -05:00
if (function_exists('mb_strtolower'))
{
return mb_strtolower($value, static::encoding());
}
return 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.
*
* @param string $value
* @return string
*/
public static function upper($value)
{
2011-09-08 17:49:16 -05:00
if (function_exists('mb_strtoupper'))
{
return mb_strtoupper($value, static::encoding());
}
return 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
/**
* Convert a string to title case (ucwords).
*
* @param string $value
* @return string
*/
public static function title($value)
{
2011-09-08 17:49:16 -05:00
if (function_exists('mb_convert_case'))
{
return mb_convert_case($value, MB_CASE_TITLE, static::encoding());
}
return 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.
*
* @param string $value
* @return int
*/
public static function length($value)
{
2011-09-08 17:49:16 -05:00
if (function_exists('mb_strlen'))
{
return mb_strlen($value, static::encoding());
}
return 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.
*
* @param string $value
* @return string
*/
public static function ascii($value)
{
$foreign = Config::get('ascii');
2011-08-08 14:31:12 -05:00
$value = preg_replace(array_keys($foreign), 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.
*
* Supported types: 'alpha_num' and 'alpha'.
*
* @param int $length
* @param string $type
* @return string
*/
public static function random($length = 16, $type = 'alpha_num')
{
2011-09-08 17:49:16 -05:00
$alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
2011-06-08 23:45:08 -05:00
2011-09-08 17:49:16 -05:00
$pool = ($type == 'alpha_num') ? '0123456789'.$alpha : $alpha;
return implode('', array_map(function() use ($pool) { return $pool[mt_rand(0, strlen($pool) - 1)]; }, range(0, $length - 1)));
2011-08-08 14:31:12 -05:00
}
2011-07-07 07:14:57 -07:00
2011-08-29 22:30:00 -05:00
/**
* Get the application encoding from the configuration class.
*
* @return string
*/
2011-09-08 17:49:16 -05:00
protected static function encoding()
2011-08-29 22:30:00 -05:00
{
return Config::get('application.encoding');
2011-08-29 22:30:00 -05:00
}
2011-06-08 23:45:08 -05:00
}