Methods This class defines the following methods:

static truncate (string $text, int $limit = 72, string $add = '...') -> string

Truncates a string to a specific length.

Also appends the value of $add (3rd argument) to the end of the string (but only if it was actually truncated).

It will also attempt to not break any words and truncate the string at the last possible space.

Example:

 echo \MD\Foundation\Utils\StringUtils::truncate('Lorem ipsum', 8);
 // -> 'Lorem...'
Arguments:
string $text

String to truncate.

int $limit

[optional] Maximum length of the string. Default: 72.

string $add

[optional] String to append at the end. Default: ....

Returns:
string

static zeroFill (string $string, int $maxLength = 4) -> string

Prefixes the given string with 0's until its length is what is given in the 2nd argument.

Example:

 echo \MD\Foundation\Utils\StringUtils::zeroFill('ab', 6);
 // -> '0000ab'
Arguments:
string $string

String to be prefixed with 0's.

int $maxLength

[optional] Target length of the string. Default: 4.

Returns:
string

static clear (string $string, bool $punctuation = true) -> string

Clears the given string of any white spaces, punctuation, new lines, tabs, etc.

Example:

 echo \MD\Foundation\Utils\StringUtils::clear('   <p>Lorem,    ipsum?</p>');
 // -> 'Lorem ipsum'
Arguments:
string $string

String to be cleared.

bool $punctuation

[optional] Remove punctuation? Default: true.

Returns:
string

static getWords (string $string, bool $removePunctuation = false) -> array

Returns an array of all words (not unique) in the given string.

Example:

 echo \MD\Foundation\Utils\StringUtils::getWords('Lorem ipsum dolor <strong>sit amet</strong>...', true);
 // -> array('Lorem', 'ipsum', 'dolor', 'sit', 'amet')
Arguments:
string $string

String to get the words from.

bool $removePunctuation

[optional] Should punctuation be removed before extracting words? Default: false.

Returns:
array

static getFirstWord (string $string) -> string

Return the first word found in the given string.

Example:

 echo \MD\Foundation\Utils\StringUtils::getFirstWord('Lorem, ipsum dolor sit amet...');
 // -> 'Lorem'
Arguments:
string $string

String to get the first word from.

Returns:
string

static getFirstSentence (string $string) -> string

Returns the first sentence from the given string, previously removing any HTML tags.

Example:

 echo \MD\Foundation\Utils\StringUtils::getFirstSentence('Lorem ipsum! Dolor sit amet...');
 // -> 'Lorem ipsum!'
Arguments:
string $string

String to get the first sentence from.

Returns:
string

static wordCount (string $string) -> int

Returns how many words are used in the string.

Example:

 echo \MD\Foundation\Utils\StringUtils::wordCount('Lorem, ipsum dolor! Sit amet...');
 // -> 5
Arguments:
string $string

String to count words for.

Returns:
int

static translit (string $string, string $encoding = 'UTF-8') -> string

Tries to remove any accents from letters in the given string by replacing them with similar looking letters.

Experimental. Use only with UTF-8 strings.

Example:

 echo \MD\Foundation\Utils\StringUtils::translit('Zażółć gęślą jaźń');
 // -> 'Zazolc gesla jazn'
Arguments:
string $string

String to be translit.

string $encoding

[optional] If you know the string's encoding then put it here. Default: UTF-8.

Returns:
string

static urlFriendly (string $string, bool $lowercase = true) -> string

Make a string that is URL (SEO) friendly.

Note that it doesn't mean it makes a valid URL - it will escape normally URL accepted characters like %, ?, / or &.

Example:

 echo \MD\Foundation\Utils\StringUtils::urlFriendly('Lorem & ipsum dolor sit amet!');
 // -> 'lorem-and-ipsum-dolor-sit-amet'
Arguments:
string $string

String to make URL friendly.

bool $lowercase

[optional] Should the string be made lowercase? Default: true.

Returns:
string

static fileNameFriendly (string $string) -> string

Make a string that is file system friendly.

Example:

 echo \MD\Foundation\Utils\StringUtils::fileNameFriendly('My Pictures (2005).zip');
 // -> 'My-pictures-(2005).zip'
Arguments:
string $string

String to make file system friendly.

Returns:
string

static fileNamePrefix (string $file, string $prefix) -> string

Adds a prefix to the filename even if a path to file has been given.

When a path to file has been given then the prefix will still be prepended to the filename itself and not the full path.

Example:

 echo \MD\Foundation\Utils\StringUtils::fileNamePrefix('/var/www/index.html', 'main.');
 // -> '/var/www/main.index.html'
Arguments:
string $file

File name. Either just the file name or a full path to file.

string $prefix

Prefix to be added.

Returns:
string

static fileNameSuffix (string $file, string $suffix) -> string

Adds a suffix to the filename without breaking its extension.

The suffix will be added before the last dot.

Example:

 echo \MD\Foundation\Utils\StringUtils::fileNameSuffix('/var/www/index.html', '.bak');
 // -> '/var/www/index.bak.html'
Arguments:
string $file

File name. Either just the file name or a full path to file.

string $suffix

Suffix to be added.

Returns:
string

static toCamelCase (string $string, string $separator = '-') -> string

Convert a string with words separated by a separator to camelCase.

Example:

 echo \MD\Foundation\Utils\StringUtils::toCamelCase('upload-file-input');
 // -> 'uploadFileInput'
Arguments:
string $string

String to be camelcased.

string $separator

[optional] Separator to use to split the string. Default: -.

Returns:
string

static toHyphenated (string $string) -> string

Deprecated! This method has been deprecated. Do not use it anymore!

Convert a string in camelCase to hyphens, e.g.

uploadFile to upload-file.

This method is deprecated. You should use ::toSeparated() instead.

Arguments:
string $string

String to be hyphenated.

Returns:
string

static toSeparated (string $string, string $separator = '-') -> string

Convert a string in camelCase to separated by the given separator.

Example:

 echo \MD\Foundation\Utils\StringUtils::toSeparated('loremIpsumDolorSitAmet');
 // -> 'lorem-ipsum-dolor-sit-amet'
Arguments:
string $string

Camelcase string to be separated with the $separator.

string $separator

[optional] Separator to use. Default: -.

Returns:
string

static isEmail (string $email) -> bool

Checks whether a string is a valid e-mail address.

Example:

 echo \MD\Foundation\Utils\StringUtils::isEmail('me@email.com');
 // -> true

 echo \MD\Foundation\Utils\StringUtils::isEmail('me@email');
 // -> false
Arguments:
string $email

Email to validate.

Returns:
bool

static isUrl (string $url) -> bool

Checks whether a string is a valid URL.

Example:

 echo \MD\Foundation\Utils\StringUtils::isUrl('http://www.php.net');
 // -> true

 echo \MD\Foundation\Utils\StringUtils::isUrl('www.php.net');
 // -> false
Arguments:
string $url

String to validate.

Returns:
bool

static isClassName (string $name, bool $allowNamespace = false) -> bool

Checks if the given string is a valid class name.

It doesn't check if the class actually exists, it just checks if the given string could be used as a class name.

Example:

 echo \MD\Foundation\Utils\StringUtils::isClassName('123Class');
 // -> false
Arguments:
string $name

Class name to validate.

bool $allowNamespace

[optional] Can the class name include a namespace? Default: false.

Returns:
bool

static fixUrlProtocol (string $url) -> string

Fix a given URL if it doesn't have http:// in front (common user mistake! :))

Example:

 echo \MD\Foundation\Utils\StringUtils::fixUrlProtocol('www.php.net');
 // -> 'http://www.php.net'
Arguments:
string $url

URL to check or fix.

Returns:
string

static random (int $length = 16, bool $capitals = true, bool $punctuation = false) -> string

Generate a random string of a given length.

It just generates a random string, but doesn't check for its uniqueness or anything like that, so you probably shouldn't use it as a unique id generator.

Arguments:
int $length

[optional] Length of the generated string. Default: 16.

bool $capitals

[optional] Should the string include capital letters? Default: true.

bool $punctuation

[optional] Should the string include special characters like punctuation? Default: false.

Returns:
string

static interpolate (string $string, mixed $variables) -> string

Interpolates the given string with variables.

Looks for occurrences of placeholders like {foo} or {bar} and replaces them with values found under keys foo or bar (respectively) in the given array of variables.

Also accepts an object as the second argument and in such case it will attempt to read the properties of that object using getters.

Example:

 echo \MD\Foundation\Utils\StringUtils::interpolate('I have a {item} and I will {action} it.', array(
     'item' => 'banana',
     'action' => 'eat'
 ));
 // -> 'I have a banana and I will eat it.'
Arguments:
string $string

String to interpolate.

mixed $variables

Either an array or an object with variables to be interpolated into the string.

Returns:
string
Throws:
MD\Foundation\Exceptions\InvalidArgumentException

If the $variables argument is neither an array or an object.

static parseVariables (string $string, mixed $variables) -> string

Deprecated! This method has been deprecated. Do not use it anymore!

Alias for ::interpolate().

Arguments:
string $string

String to interpolate.

mixed $variables

Either an array or an object with variables to be interpolated into the string.

Returns:
string

static secondsToTimeString (int $seconds, bool $hideHoursWhenZero = false) -> string

Changes the given number of seconds to a format h:mm:ss.

Example:

 echo \MD\Foundation\Utils\StringUtils::secondsToTimeString(3785);
 // -> '1:03:05'
Arguments:
int $seconds

Number of seconds to be transformed.

bool $hideHoursWhenZero

[optional] If the time returned has zero hours then do not display them. Default: false.

Returns:
string

static timeStringToSeconds (string $string) -> int

Changes string of time format h:mm:ss to number of seconds.

Reverse of ::secondsToTimeString().

Can also take . (dot) as a separator.

Example:

 echo \MD\Foundation\Utils\StringUtils::timeStringToSeconds('1:03:05');
 // -> 3785
Arguments:
string $string

Formated time string to be converted to seconds.

Returns:
int

static bytesToString (int $bytes) -> string

Changes the given bytes number to a human readable string.

Example:

 echo \MD\Foundation\Utils\StringUtils::bytesToString(1572864);
 // -> '1.5 MB'
Arguments:
int $bytes

Number of bytes to be converted to a human readable string.

Returns:
string

static hexToRgb (string $hex) -> array

Converts the given hex color to RGB color.

Returns an array where index 0 = red, 1 = green, 2 = blue.

Accepts hex colors with and without the # in front (ff0000 and #ff0000) as well as short notation (#f00).

Example:

 echo \MD\Foundation\Utils\StringUtils::hexToRgb('#B8860B');
 // -> array(184, 134, 11)
Arguments:
string $hex

Hex color that you want to convert. Can have a # in front.

Returns:
array

static rgbToHex (array|string $rgb, string $separator = ',') -> string

Converts the given RGB value (either as a separated string or an array) to hex value.

Returns the hex value in lowercase, without appended #.

Example:

 echo \MD\Foundation\Utils\StringUtils::rgbToHex(array(255, 182, 193));
 // -> 'ffb6c1'

 echo \MD\Foundation\Utils\StringUtils::rgbToHex('75, 0, 130');
 // -> '4b0082'

 echo \MD\Foundation\Utils\StringUtils::rgbToHex('189:183:107', ':');
 // -> 'bdb76b'
Arguments:
array|string $rgb

Either a zero-indexed array of red, green and blue values or a string separated by $separator.

string $separator

[optional] If $rgb is a string then what's the separator? Default: ,.

Returns:
string

static timeAgo (int $timestamp, int $levels = 1, mixed $returnDateIfOlder = '3 weeks ago', string $returnDateFormat = 'd.m.Y H:i', bool $secondSpecific = false, bool $trimAgo = false) -> string

Changes the given UNIX timestamp to a string saying 'xxx ago'.

Arguments:
int $timestamp

UNIX timestamp.

int $levels

[optional] How many levels of time periods to show.

mixed $returnDateIfOlder

[optional] If specified then the function will return a regular date instead of 'xxx ago' if the date is older than this. Use false if you want to ommit this functionality. To use it pass any string that can be used by strtotime(). Default: 3 weeks ago.

string $returnDateFormat

[optional] If $returnDateIfOlder is set then this is the format in which the given date will be returned. Parsing of this will be rerouted to date() and this is the format that will be passed to that function. Default: d.m.Y H:i.

bool $secondSpecific

[optional] Should the return be specific to a second? Otherwise will return 'few seconds ago'. Defualt: false.

bool $trimAgo

[optional] Should the 'ago' appendix be not added? Mainly for internal use. Default: false.

Returns:
string

static stripHtml (string $string) -> string

Strips HTML tags and encodes HTML special chars from a string.

Arguments:
string $string

String to be stripped of HTML.

Returns:
string

static multiExplode (string $standardDelimeter, string $string, array $delimeters = array()) -> array

Explodes the given string just like php's explode() function with additional possibility of multiple exploding delimeters.

Arguments:
string $standardDelimeter

Standard delimeter by which explode the string.

string $string

String to explode.

array $delimeters

[optional] Array of any other delimeters to take into account while exploding.

Returns:
array