String

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 useful.

Just remember to use Utilities\Str.

# Sanitize

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.

# Others

contains

Returns true if a string contains a substring.

Str::contains('Lorem ipsum dolor sit amet', 'sit');
//Returns true

interpolate

Returns a string with its placeholders replaced by context values.

$values = array(
    'first' => 'john',
    'last'  => 'doe'
);

Str::interpolate('Your firstname is {first} and your lastname is {last}', $values);

//Returns 'Your firstname is john and your lastname is doe'.

The first parameter is the string, the second one is the array with the placeholders values. The placeholders in the string should have this {key} format.

toUtf8

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

limit

Returns a truncated string with the specified length.

Str::limit('Lorem ipsum dolor sit amet', '4');
//Returns 'Lore'

unShift

Adds the given value at the start of the string and returns it.

Str::unShift('psum dolor sit amet', 'Lorem i');
//Returns 'Lorem ipsum dolor sit amet'

concat

Returns all the given strings concatenated into one.

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

toString

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.

startsWith

Returns true if a string starts with a substring.

Str::startsWith('Lorem ipsum dolor sit amet', 'Lorem');
//Returns true

endsWith

Returns true if a string ends with a substring.

Str::endsWith('Lorem ipsum dolor sit amet', 'amet');
//Returns true

remove

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');
//Returns 'Lorem ipsum dolor  amet'

after

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');
//Returns ' sit amet'

before

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');
//Returns 'Lorem ipsum '

pathToNamespace

Turns a directory path into a namespace path, it replaces slashes by backslashes.

pathToNamespace('sub/home');
//Returns sub\home

namespaceToPath

Turns a namespace path into a directory path, it replaces backslashes by slashes.

namespaceToPath('sub\home');
//Returns sub/home