In PHP, using functions related to strings is quite common, that's why Wolff includes a class with some functions related to strings which can be quite useful.
Just remember to use Utilities\Str
.
Sanitize strings is an important thing to do. So the String class have some functions related to it.
Sanitizing an URL:
$url = Str::sanitizeURL($url);
//The function will return the $url variable sanitized
Exactly the same can be applied with the sanitizeEmail
, sanitizeInt
, sanitizeFloat
and sanitizePath
functions.
Returns true if the given string complies with an email format.
Str::isEmail('[email protected]');
That will return true.
Returns true if the given string contains only alphanumeric characters and whitespaces.
Str::isAlphanumeric('abcdefg1234567 890');
That will return true.
Returns true if the given string contains only letters and whitespaces.
Str::isAlpha('abc def g');
That will return true.
Returns a random generated token.
Str::token();
The default length of the token is 16 characters, but it can be changed passing a number as the only one parameter to that method.
Str::token(24);
That will return a token with 24 characters length.
Returns a url friendly string.
Str::slug(' Hola cómo estás? Bien');
That will return hola-como-estas-bien
.
Basically this function replaces special letters by their normal counterpart, puts everything lowercase and replaces the remaining characters with an hyphen -
.
Returns true if a string contains a substring.
Str::contains('Lorem ipsum dolor sit amet', 'sit');
That will return true.
Returns a string with its placeholders replaced by context values.
$values = [
'first' => 'john',
'last' => 'doe'
];
Str::interpolate('Your firstname is {first} and your lastname is {last}', $values);
That will return Your firstname is john and your lastname is doe
.
If the given array is empty, the method will return the original string.
Returns a string with the two indicated substrings swapped.
Str::swap("I'm the Alpha, the Omega, everything in between", "Alpha", "Omega");
That will return I'm the Omega, the Alpha, everything in between
.
The first parameter is the string, the remaining two are the substrings to be swapped.
Converts a string with any encoding to UTF-8 and returns it.
Keep in mind that the string encoding detection is not perfect.
Str::toUtf8($string);
Returns a truncated string with the specified length.
Str::limit('Lorem ipsum dolor sit amet', '4');
That will return Lore
.
Adds the given value at the start of the string and returns it.
Str::unShift('psum dolor sit amet', 'Lorem i');
That will return Lorem ipsum dolor sit amet
.
Returns all the given strings and/or arrays of strings concatenated as a path.
Str::concatPath('home', 'public', 'logo.svg');
Str::concatPath(['home', 'public'], 'logo.svg');
Both examples are the same and will return home/public/logo.svg
.
Returns all the given strings concatenated into one.
Str::concat('Lorem ', 'ipsum ', 'dolor');
Returns Lorem ipsum dolor
.
Returns the given value as a string.
Str::toString(true);
//Returns 'true'
Booleans will be converted to a 'true' or 'false' string.
Arrays will be imploded to a normal string.
Numeric values will be converted to a string using the strval
function.
Returns true if a string starts with a substring.
Str::startsWith('Lorem ipsum dolor sit amet', 'Lorem');
That will return true
.
Returns true if a string ends with a substring.
Str::endsWith('Lorem ipsum dolor sit amet', 'amet');
That will return true
.
Removes all the ocurrences of a subtring in a string.
The first parameter is the original string, the second is the substring to remove.
Str::remove('Lorem ipsum dolor sit amet', 'sit');
That will return Lorem ipsum dolor amet
.
Returns everything after the specified substring.
The first parameter is the original string, the second is the substring that will cut it.
Str::after('Lorem ipsum dolor sit amet', 'dolor');
That will return sit amet
.
Returns everything before the specified substring.
The first parameter is the original string, the second is the substring that will cut it.
Str::before('Lorem ipsum dolor sit amet', 'dolor');
That will return Lorem ipsum
.
Turns a directory path into a namespace path, it replaces slashes by backslashes.
pathToNamespace('sub/home');
That will return sub\home
.
Turns a namespace path into a directory path, it replaces backslashes by slashes.
namespaceToPath('sub\home');
That will return sub/home
.