Str Class

The str class is a set of methods to help with the manipulation of strings.

increment($str, $first = 1)

The increment method allows you to append a number to the end of a string or increment that number if it already exists.

Static Yes
Parameters
Param Default Description
$string required The string to increment.
$first
1
The first number to increment with.
Returns string
Example
$string = "filename";
Str::increment($string); // returns filename_1

$string = "filename_1";
Str::increment($string); // returns filename_2

$string = "filename";
Str::increment($string, 3); // returns filename_3

random($type = 'alnum', $length = 8)

The random method generates a random string based on the type given.

Static Yes
Parameters
Param Default Description
$type alnum The type of string to generate. Your choices are alnum, numeric, nozero, alpha, distinct, hexdec, unique, and sha1.
$length
16
The number of characters you would like the final string to be (unique and sha1 ignore this parameter).
Returns string
Example
// alnum (uppercase and lowercase letters mixed with numbers)
Str::random('alnum', 16);
// Returns: SvZi9Dh3lq7zQYim

// numeric (just numbers)
Str::random('numeric', 16);
// Returns: 1045343964672481

// nozero (just numbers excluding zero)
Str::random('nozero', 16);
// Returns: 3244623373994515

// alpha (just uppercase and lowercase letters)
Str::random('alpha', 16);
// Returns: LuVAXbmxQbbWoYqz

// distinct (uppercase letters and numbers that cannot be confused)
Str::random('distinct', 16);
// Returns: R79MPKMH4KTRN35J

// hexdec (hexadecimal characters a-f, 0-9)
Str::random('hexdec', 16);
// Returns: 09c34e42f36547f8

// unique (32 character string based on md5)
Str::random('unique');
// Returns: ed4bb844a35b7a4edb7eed0d3795d328

// sha1 (40 character string based on sha1)
Str::random('sha1');
// Returns: af5c5a8cc3be9a3180205c1ed2975015cd6cf1e7

truncate($string, $limit, $continuation = '...', $is_html = false)

The truncate method allows you to limit characters and provide a continuation string without breaking html.

Static Yes
Parameters
Param Default Description
$string required The string to truncate.
$limit required The number of characters to allow in the string.
$continuation
'...'
The string to append to the end of the truncated string.
$is_html
false
If the string contains html. Setting this as true will make the method not break html.
Returns string
Example
$string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
Str::truncate($string, 15); // returns Lorem ipsum dol...

$string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
Str::truncate($string, 15, '...Read More'); // returns Lorem ipsum dol...Read More

alternator(*$args)

Returns a closure that will alternate the values you've passed to this method as arguments, unless you call the closure with false as argument - in which case it will just return the value without moving to the next and return the same value on the next call.

Static Yes
Parameters
Param Default Description
*$args required As many arguments as you need to alternate
Returns Closure
Example
$alt = Str::alternator('one', 'two', 'three', 'four');
echo $alt(); // outputs 'one'
echo $alt(); // outputs 'two'
echo $alt(false); // outputs 'three', but doens't move to the next as you can see in the next call
echo $alt(); // outputs 'three'
echo $alt(); // outputs 'four'
echo $alt(); // outputs 'one'
// etc...

lower($str, $encoding = null)

The lower method converts all characters to lowercase. It is equivalent to PHP's strtolower() for your specific character encoding.

Static Yes
Parameters
Param Type Default Description
$string string Required The input string.
$encoding string
null
The character encoding.
Returns string
Example
Str::lower('User Data'); // returns 'user data'

tr($string, $array = array())

The tr method parse the params from given string using PHP's strtr().

Static Yes
Parameters
Param Type Default Description
$string string Required The input string.
$array array
array()
params to str_replace.
Returns string
Example
Str::tr('Hello :name', array('name' => 'World')); // returns 'Hello World'