# String

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.

General Methods

Sanitize

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);

Is Email

isEmail(string $email)

Returns true if the given string complies with an email format, false otherwise.

Str::isEmail('[email protected]');

That will return true.

Is Alphanumeric

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.

Is Alpha

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.

Remove quotes

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

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

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

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

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

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.

To UTF8

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

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.

Concatenate Path

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.

Concatenate

concat(...$strings)

Returns all the given strings concatenated into one.

Str::concat('Lorem ', 'ipsum ', 'dolor');

Returns Lorem ipsum dolor.

To String

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.

Starts with

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.

Ends with

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 substring

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 substring

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 substring

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 .

Documentation made with
Something is wrong in here? Make a issue/pull request on the repo.