str.php 4.54 KB
Newer Older
Taylor Otwell committed
1
<?php namespace Laravel;
2 3 4 5

class Str {

	/**
Taylor Otwell committed
6 7
	 * Convert a string to lowercase.
	 *
8 9 10 11 12 13 14 15
	 * <code>
	 *		// Convert a string to lowercase
	 *		echo Str::lower('STOP YELLING');
	 *
	 *		// Convert a UTF-8 string to lowercase
	 *		echo Str::lower('Τάχιστη');
	 * </code>
	 *
Taylor Otwell committed
16 17 18 19 20
	 * @param  string  $value
	 * @return string
	 */
	public static function lower($value)
	{
21 22 23 24 25 26
		if (function_exists('mb_strtolower'))
		{
			return mb_strtolower($value, Config::get('application.encoding'));
		}

		return strtolower($value);
Taylor Otwell committed
27
	}
28

Taylor Otwell committed
29 30 31
	/**
	 * Convert a string to uppercase.
	 *
32 33 34 35 36 37 38 39
	 * <code>
	 *		// Convert a string to uppercase
	 *		echo Str::upper('speak louder');
	 *
	 *		// Convert a UTF-8 string to uppercase
	 *		echo Str::upper('Τάχιστη');
	 * </code>
	 *
Taylor Otwell committed
40 41 42 43 44
	 * @param  string  $value
	 * @return string
	 */
	public static function upper($value)
	{
45 46 47 48 49 50
		if (function_exists('mb_strtoupper'))
		{
			return mb_strtoupper($value, Config::get('application.encoding'));
		}

		return strtoupper($value);
Taylor Otwell committed
51
	}
52

Taylor Otwell committed
53
	/**
54 55 56 57 58 59 60 61 62
	 * 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>
Taylor Otwell committed
63 64 65 66 67 68
	 *
	 * @param  string  $value
	 * @return string
	 */
	public static function title($value)
	{
69 70 71 72 73 74
		if (function_exists('mb_convert_case'))
		{
			return mb_convert_case($value, MB_CASE_TITLE, Config::get('application.encoding'));
		}

		return ucwords(strtolower($value));
Taylor Otwell committed
75
	}
76

Taylor Otwell committed
77 78 79
	/**
	 * Get the length of a string.
	 *
80 81 82 83 84 85 86 87
	 * <code>
	 *		// Get the length of a string
	 *		echo Str::length('taylor otwell');
	 *
	 *		// Get the length of a UTF-8 string
	 *		echo Str::length('Τάχιστη αλώπηξ');
	 * </code>
	 *
Taylor Otwell committed
88 89 90 91 92
	 * @param  string  $value
	 * @return int
	 */
	public static function length($value)
	{
93 94 95 96 97 98
		if (function_exists('mb_strlen'))
		{
			return mb_strlen($value, Config::get('application.encoding'));
		}

		return strlen($value);
Taylor Otwell committed
99
	}
100

Taylor Otwell committed
101
	/**
102 103
	 * Limit the number of characters in a string.
	 *
104
	 * <code>
105
	 *		// Returns "Tay..."
106 107 108 109
	 *		echo Str::limit('Taylor Otwell', 3);
	 *
	 *		// Limit the number of characters and append a custom ending
	 *		echo Str::limit('Taylor Otwell', 3, '---');
110 111 112
	 * </code>
	 *
	 * @param  string  $value
113
	 * @param  int     $limit
114 115 116
	 * @param  string  $end
	 * @return string
	 */
117
	public static function limit($value, $limit = 100, $end = '...')
118
	{
119
		if (static::length($value) <= $limit) return $value;
120

121 122 123 124
		if (function_exists('mb_substr'))
		{
			return mb_substr($value, 0, $limit, Config::get('application.encoding')).$end;
		}
125

126
		return substr($value, 0, $limit).$end;
127 128 129 130 131 132
	}

	/**
	 * Limit the number of words in a string
	 *
	 * <code>
133 134 135 136 137
	 *		// Returns "This is a..."
	 *		echo Str::words('This is a sentence.', 3);
	 *
	 *		// Limit the number of words and append a custom ending
	 *		echo Str::words('This is a sentence.', 3, '---');
138 139 140 141 142 143 144
	 * </code>
	 *
	 * @param  string  $value
	 * @param  int     $length
	 * @param  string  $end
	 * @return string
	 */
145
	public static function words($value, $words = 100, $end = '...')
146
	{
147
		$count = str_word_count($value, 1);
148

149
		if ($count <= $words) return $value;
150

151
		return implode(' ', array_slice($count, 0, $words)).$end;
152 153 154
	}

	/**
Taylor Otwell committed
155 156
	 * Convert a string to 7-bit ASCII.
	 *
157 158 159 160 161
	 * <code>
	 *		// Returns "Deuxieme Article"
	 *		echo Str::ascii('Deuxième Article');
	 * </code>
	 *
Taylor Otwell committed
162 163 164 165 166
	 * @param  string  $value
	 * @return string
	 */
	public static function ascii($value)
	{
167 168 169
		$foreign = Config::get('ascii');

		$value = preg_replace(array_keys($foreign), array_values($foreign), $value);
170

Taylor Otwell committed
171 172
		return preg_replace('/[^\x09\x0A\x0D\x20-\x7E]/', '', $value);
	}
173

Taylor Otwell committed
174 175 176
	/**
	 * Generate a random alpha or alpha-numeric string.
	 *
177 178 179 180 181 182 183
	 * <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>
Taylor Otwell committed
184
	 *
Taylor Otwell committed
185
	 * @param  int	   $length
Taylor Otwell committed
186 187 188
	 * @param  string  $type
	 * @return string
	 */
Taylor Otwell committed
189
	public static function random($length, $type = 'alnum')
Taylor Otwell committed
190
	{
191 192
		return substr(str_shuffle(str_repeat(static::pool($type), 5)), 0, $length);
	}
193

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	/**
	 * Get the character pool for a given type of random string.
	 *
	 * @param  string  $type
	 * @return string
	 */
	protected static function pool($type)
	{
		switch ($type)
		{
			case 'alpha':
				return 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

			case 'alnum':
				return '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

			default:
				throw new \Exception("Invalid random string type [$type].");
		}
Taylor Otwell committed
213
	}
Taylor Otwell committed
214

215
}