| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 | <?php namespace Laravel;class Str {	/**	 * Get the default string encoding for the application.	 *	 * This method is simply a short-cut to Config::get('application.encoding').	 *	 * @return string	 */	public static function encoding()	{		return Config::get('application.encoding');	}	/**	 * Get the length of a string.	 *	 * <code>	 *		// Get the length of a string	 *		$length = Str::length('Taylor Otwell');	 *	 *		// Get the length of a multi-byte string	 *		$length = Str::length('Τάχιστη')	 * </code>	 *	 * @param  string  $value	 * @return int	 */	public static function length($value)	{		return (MB_STRING) ? mb_strlen($value, static::encoding()) : strlen($value);	}	/**	 * Convert a string to lowercase.	 *	 * <code>	 *		// Convert a string to lowercase	 *		$lower = Str::lower('Taylor Otwell');	 *	 *		// Convert a multi-byte string to lowercase	 *		$lower = Str::lower('Τάχιστη');	 * </code>	 *	 * @param  string  $value	 * @return string	 */	public static function lower($value)	{		return (MB_STRING) ? mb_strtolower($value, static::encoding()) : strtolower($value);	}	/**	 * Convert a string to uppercase.	 *	 * <code>	 *		// Convert a string to uppercase	 *		$upper = Str::upper('Taylor Otwell');	 *	 *		// Convert a multi-byte string to uppercase	 *		$upper = Str::upper('Τάχιστη');	 * </code>	 *	 * @param  string  $value	 * @return string	 */	public static function upper($value)	{		return (MB_STRING) ? mb_strtoupper($value, static::encoding()) : strtoupper($value);	}	/**	 * Convert a string to title case (ucwords equivalent).	 *	 * <code>	 *		// Convert a string to title case	 *		$title = Str::title('taylor otwell');	 *	 *		// Convert a multi-byte string to title case	 *		$title = Str::title('νωθρού κυνός');	 * </code>	 *	 * @param  string  $value	 * @return string	 */	public static function title($value)	{		if (MB_STRING)		{			return mb_convert_case($value, MB_CASE_TITLE, static::encoding());		}		return ucwords(strtolower($value));	}	/**	 * Limit the number of characters in a string.	 *	 * <code>	 *		// Returns "Tay..."	 *		echo Str::limit('Taylor Otwell', 3);	 *	 *		// Limit the number of characters and append a custom ending	 *		echo Str::limit('Taylor Otwell', 3, '---');	 * </code>	 *	 * @param  string  $value	 * @param  int     $limit	 * @param  string  $end	 * @return string	 */	public static function limit($value, $limit = 100, $end = '...')	{		if (static::length($value) <= $limit) return $value;		if (MB_STRING)		{			return mb_substr($value, 0, $limit, static::encoding()).$end;		}		return substr($value, 0, $limit).$end;	}	/**	 * Get the singular form of the given word.	 *	 * The word should be defined in the "strings" configuration file.	 *	 * @param  string  $value	 * @return string	 */	public static function singular($value)	{		return array_get(array_flip(Config::get('strings.inflection')), strtolower($value), $value);	}	/**	 * Get the plural form of the given word.	 *	 * The word should be defined in the "strings" configuration file.	 *	 * <code>	 *		// Returns the plural form of "child"	 *		$plural = Str::plural('child', 10);	 *	 *		// Returns the singular form of "octocat" since count is one	 *		$plural = Str::plural('octocat', 1);	 * </code>	 *	 * @param  string  $value	 * @return string	 */	public static function plural($value, $count = 2)	{		if ((int) $count == 1) return $value;		return array_get(Config::get('strings.inflection'), strtolower($value), $value);	}	/**	 * Limit the number of words in a string.	 *	 * <code>	 *		// 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, '---');	 * </code>	 *	 * @param  string  $value	 * @param  int     $length	 * @param  string  $end	 * @return string	 */	public static function words($value, $words = 100, $end = '...')	{		preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/', $value, $matches);		if (static::length($value) == static::length($matches[0]))		{			$end = '';		}		return rtrim($matches[0]).$end;	}	/**	 * Generate a URL friendly "slug" from a given string.	 *	 * <code>	 *		// Returns "this-is-my-blog-post"	 *		$slug = Str::slug('This is my blog post!');	 *	 *		// Returns "this_is_my_blog_post"	 *		$slug = Str::slug('This is my blog post!', '_');	 * </code>	 *	 * @param  string  $title	 * @param  string  $separator	 * @return string	 */	public static function slug($title, $separator = '-')	{		$title = static::ascii($title);		// Remove all characters that are not the separator, letters, numbers, or whitespace.		$title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', static::lower($title));		// Replace all separator characters and whitespace by a single separator		$title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);		return trim($title, $separator);	}	/**	 * Convert a string to 7-bit ASCII.	 *	 * This is helpful for converting UTF-8 strings for usage in URLs, etc.	 *	 * @param  string  $value	 * @return string	 */	public static function ascii($value)	{		$foreign = Config::get('strings.ascii');		$value = preg_replace(array_keys($foreign), array_values($foreign), $value);		return preg_replace('/[^\x09\x0A\x0D\x20-\x7E]/', '', $value);	}	/**	 * Convert a string to an underscored, camel-cased class name.	 *	 * This method is primarily used to format task and controller names.	 *	 * <code>	 *		// Returns "Task_Name"	 *		$class = Str::classify('task_name');	 *	 *		// Returns "Taylor_Otwell"	 *		$class = Str::classify('taylor otwell')	 * </code>	 *	 * @param  string  $value	 * @return string	 */	public static function classify($value)	{		$search = array('_', '-', '.');		return str_replace(' ', '_', static::title(str_replace($search, ' ', $value)));	}	/**	 * Return the "URI" style segments in a given string.	 *	 * @param  string  $value	 * @return array	 */	public static function segments($value)	{		return array_diff(explode('/', trim($value, '/')), array(''));	}	/**	 * Generate a random alpha or alpha-numeric string.	 *	 * <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	 * @return string	 */	public static function random($length, $type = 'alnum')	{		return substr(str_shuffle(str_repeat(static::pool($type), 5)), 0, $length);	}	/**	 * 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].");		}	}}
 |