mako\String


Description


Collection of string manipulation methods.


Class methods


Toggle source

protected __construct()


Protected constructor since this is a static class.

Return value

NULL

protected function __construct()
{
	// Nothing here
}

Toggle source

public static nl2br($string)


Replaces newline with <br> or <br />.


Parameters

Type Description
string The input string
Return value

string

public static function nl2br($string)
{
	return str_replace(array("\r\n", "\n\r", "\n", "\r"), HTML::tag('br'), $string);
}

Toggle source

public static br2nl($string)


Replaces <br> and <br /> with newline.


Parameters

Type Description
string The input string
Return value

string

public static function br2nl($string)
{
	return str_replace(array('<br>', '<br/>', '<br />'), "\n", $string);
}

Toggle source

public static camel2underscored($string)


Converts camel case to underscored.


Parameters

Type Description
string The input string
Return value

string

public static function camel2underscored($string)
{
	return mb_strtolower(preg_replace('/([^A-Z])([A-Z])/u', "$1_$2", $string));
}

Toggle source

public static underscored2camel($string, $upper = false)


Converts underscored to camel case.


Parameters

Type Description
string The input string
Return value

string

public static function underscored2camel($string, $upper = false)
{
	return preg_replace_callback(($upper ? '/(?:^|_)(.?)/u' : '/_(.?)/u'), function($matches){ return mb_strtoupper($matches[1]); }, $string);
}

Toggle source

public static limitChars($string, $characters = 100, $sufix = '...')


Limits the number of characters in a string.


Parameters

Type Description
string The input string
int (optional) Number of characters to allow
string (optional) Sufix to add if number of characters is reduced
Return value

NULL

public static function limitChars($string, $characters = 100, $sufix = '...')
{
	return (mb_strlen($string) > $characters) ? trim(mb_substr($string, 0, $characters)) . $sufix : $string;
}

Toggle source

public static limitWords($string, $words = 100, $sufix = '...')


Limits the number of words in a string.


Parameters

Type Description
string The input string
int (optional) Number of words to allow
string (optional) Sufix to add if number of words is reduced
Return value

NULL

public static function limitWords($string, $words = 100, $sufix = '...')
{
	preg_match('/^\s*+(?:\S++\s*+){1,' . $words . '}/', $string, $matches);

	if(isset($matches[0]) && mb_strlen($matches[0]) < mb_strlen($string))
	{
		return trim($matches[0]) . $sufix;
	}

	return $string;
}

Toggle source

public static slug($string)


Creates url friendly string.


Parameters

Type Description
string The input string
Return value

string

public static function slug($string)
{
	return mb_strtolower(preg_replace('/\s{1,}/', '-', trim(preg_replace('/[\x21-\x2F\x3A-\x40\x5B-\x60\x7B-\x7E]/', '', $string))));
}

Toggle source

public static ascii($string)


Strips all non-ASCII characters.


Parameters

Type Description
string The input string
Return value

string

public static function ascii($string)
{
	return preg_replace('/[^\x0-\x7F]/', '', $string);
}

Toggle source

public static alternate($strings, $reset = false)


Alternates between two or more strings.


Parameters

Type Description
array Array of strings to alternate between
boolean (optional) Reset alternator?
Return value

string

public static function alternate(array $strings, $reset = false)
{
	static $i = 0;

	$reset && $i = 0;

	return $strings[($i++ % count($strings))];
}

Toggle source

public static autolink($string, $attributes = array ( ))


Converts URLs in a text into clickable links.


Parameters

Type Description
string Text to scan for links
boolean (optional) Anchor attributes
Return value

string

public static function autolink($string, array $attributes = array())
{
	return preg_replace_callback('#\b(?<!href="|">)[a-z]+://\S+(?:/|\b)#i', function($matches) use ($attributes)
	{
		return HTML::tag('a', array('href' => $matches[0]) + $attributes, $matches[0]);
	}, $string);
}

Toggle source

public static mask($string, $visible = 3, $mask = '*')


Returns a masked string where only the last n characters are visible.


Parameters

Type Description
string String to mask
int (optional) Number of characters to show
string (optional) Character used to replace remaining characters
Return value

string

public static function mask($string, $visible = 3, $mask = '*')
{
	$substr = mb_substr($string, -$visible);

	return str_pad($substr, (mb_strlen($string) + (strlen($substr) - mb_strlen($substr))), $mask, STR_PAD_LEFT);
}

Toggle source

public static increment($string, $start = 1, $separator = '_')


Increments a string by appending a number to it or increasing the number.


Parameters

Type Description
string String to increment
int Starting number
string Separator
Return value

string

public static function increment($string, $start = 1, $separator = '_')
{
	preg_match('/(.+)' . preg_quote($separator) . '([0-9]+)$/', $string, $matches);

	return isset($matches[2]) ? $matches[1] . $separator . ((int) $matches[2] + 1) : $string . $separator . $start;
}

Toggle source

public static random($pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', $length = 32)


Returns a random string of the selected type and length.


Parameters

Type Description
string Character pool to use
int (optional) Desired string length
Return value

string

public static function random($pool = String::ALNUM, $length = 32)
{
	$string = '';

	$poolSize = mb_strlen($pool) - 1;

	for($i = 0; $i < $length; $i++)
	{
		$string .= mb_substr($pool, mt_rand(0, $poolSize), 1);
	}

	return $string;
}