Str Class
The str class is a set of methods to help with the manipulation of strings.
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);
$string = "filename_1";
Str::increment($string);
$string = "filename";
Str::increment($string, 3);
|
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, sha1 and uuid. |
$length |
16
|
The number of characters you would like the final string to be (unique, sha1 and uuid ignore this parameter). |
|
Returns |
string |
Example |
Str::random('alnum', 16);
Str::random('numeric', 16);
Str::random('nozero', 16);
Str::random('alpha', 16);
Str::random('distinct', 16);
Str::random('hexdec', 16);
Str::random('unique');
Str::random('sha1');
Str::random('uuid');
|
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);
$string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
Str::truncate($string, 15, '...Read More');
|
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();
echo $alt();
echo $alt(false);
echo $alt();
echo $alt();
echo $alt();
|
The upper method converts all characters to uppercase. It is equivalent to PHP's strtoupper() 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::upper('User Data');
|
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');
|
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'));
|
The is_json method check if a string is json encoded.
Static |
Yes |
Parameters |
Param |
Type |
Default |
Description |
$string |
string |
Required |
The input string. |
|
Returns |
bool |
Example |
Str::is_json('{"0":"An","encoded":["string"]}');
|
The is_serialized method check if a string is serialized.
Static |
Yes |
Parameters |
Param |
Type |
Default |
Description |
$string |
string |
Required |
The input string. |
|
Returns |
bool |
Example |
Str::is_serialized('a:2:{i:0;s:2:"An";s:7:"encoded";a:1:{i:0;s:6:"string";}}');
|
The is_html method check if a string is html.
Static |
Yes |
Parameters |
Param |
Type |
Default |
Description |
$string |
string |
Required |
The input string. |
|
Returns |
bool |
Example |
Str::is_html('Lorem ipsum <b>dolor sit amet</b>, consectetur <u>adipiscing</u> elit.');
|
The is_xml method check if a string is a valid xml. Requires the libxml extension.
Static |
Yes |
Parameters |
Param |
Type |
Default |
Description |
$string |
string |
Required |
The input string. |
|
Returns |
bool |
Example |
Str::is_xml('<?xml version="1.0" encoding="utf-8"?><xml><foo>bar</foo></xml>');
|
The starts_with method checks whether a string has a precific beginning.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$str |
required |
The string to check. |
$start |
required |
The beginning to check for. |
$ignore_case |
false
|
Whether to ignore the case. |
|
Returns |
bool |
Example |
$string = "Lorem ipsum dolor sit amet";
Str::starts_with($string, 'Lorem');
Str::starts_with($string, 'lorem');
Str::starts_with($string, 'lorem', true);
|
The ends_with method checks whether or not a string has a precific ending.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$str |
required |
The string to check. |
$end |
required |
The ending to check for. |
$ignore_case |
false
|
Whether to ignore the case. |
|
Returns |
bool |
Example |
$string = "Lorem ipsum dolor sit amet";
Str::ends_with($string, 'amet');
Str::ends_with($string, 'Amet');
Str::ends_with($string, 'Amet', true);
|
The sub method returns the portion of the string specified. It is equivalent to PHP's substr() for your specific character encoding.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$str |
required |
The input string. |
$start |
required |
Position of first character. See php.net for detailed usage. |
$length |
null
|
Maximum number of characters. See php.net for detailed usage. |
$encoding |
null
|
The character encoding. |
|
Returns |
string |
Example |
Str::sub('User Data', 3, -1);
|