Wolff\Utils\Str
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.
Sanitize strings is an important thing to do. So the String class have some functions related to it.
sanitizeUrl(string $url)
sanitizeEmail(string $email)
sanitizeInt(string $int)
sanitizeFloat(string $float)
sanitizePath(string $path)
Example:
$url = Str::sanitizeURL($url);
isEmail(string $email)
Returns true
if the given string complies with an email format, false
otherwise.
Str::isEmail('[email protected]');
That will return true
.
isAlphanumeric(string $str)
Returns true
if the given string contains only alphanumeric characters and whitespaces, false
otherwise.
Str::isAlphanumeric('abcdefg1234567 890');
That will return true
.
isAlpha(string $str)
Returns true
if the given string contains only letters and whitespaces, false
otherwise.
Str::isAlpha('abc def g');
That will return true.
removeQuotes(string $str)
Returns the given string without the single or double quotes surrounding it.
Str::removeQuotes('"Hello world"');
That should return the string 'Hello world'.
Keep in mind that the quotes will be removed only if they surround the string on both sides, meaning that passing a string like 'Hello world"' will make the method return the same string.
token([int $length])
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.
slug(string $str)
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 -
.
contains(string $str, string $needle)
Returns true
if a string contains a substring, false
otherwise.
Str::contains('Lorem ipsum dolor sit amet', 'sit');
That will return true
.
interpolate(string $str, array $values)
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.
swap(string $str, string $first_str, string $second_str)
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.
toUtf8(string $str)
Returns the given string from any encoding to UTF-8.
Keep in mind that the string encoding detection is not perfect.
Str::toUtf8($string);
limit(string $str, int $limit)
Returns a truncated string with the specified length.
Str::limit('Lorem ipsum dolor sit amet', '4');
That will return Lore
.
concatPath(...$paths)
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
.
concat(...$strings)
Returns all the given strings concatenated into one.
Str::concat('Lorem ', 'ipsum ', 'dolor');
Returns Lorem ipsum dolor
.
toString($var)
Returns the given value as a string.
Str::toString(true);
That will return '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.
startsWith(string $str, string $needle)
Returns true
if a string starts with a substring, false
otherwise.
Str::startsWith('Lorem ipsum dolor sit amet', 'Lorem');
That will return true
.
endsWith(string $str, string $needle)
Returns true
if a string ends with a substring, false
otherwise.
Str::endsWith('Lorem ipsum dolor sit amet', 'amet');
That will return true
.
remove(string $str, string $needle)
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
.
after(string $str, string $needle)
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
.
before(string $str, string $needle)
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
.