Text
» Kohana_Text

Text helper class.

package
Kohana
author
Kohana Team
copyright
© 2007-2008 Kohana Team
license
http://kohanaphp.com/license.html

Methods

public static limit_words ( string $str, integer $limit = 100, string $end_char = NULL )

Limits a phrase to a given number of words.

Returns:
  • string
Source:
public static function limit_words($str, $limit = 100, $end_char = NULL)
{
	$limit = (int) $limit;
	$end_char = ($end_char === NULL) ? '…' : $end_char;

	if (trim($str) === '')
		return $str;

	if ($limit <= 0)
		return $end_char;

	preg_match('/^\s*+(?:\S++\s*+){1,'.$limit.'}/u', $str, $matches);

	// Only attach the end character if the matched string is shorter
	// than the starting string.
	return rtrim($matches[0]).(strlen($matches[0]) === strlen($str) ? '' : $end_char);
}

public static limit_chars ( string $str, integer $limit = 100, string $end_char = NULL, boolean $preserve_words = false )

Limits a phrase to a given number of characters.

Returns:
  • string
Source:
public static function limit_chars($str, $limit = 100, $end_char = NULL, $preserve_words = FALSE)
{
	$end_char = ($end_char === NULL) ? '…' : $end_char;

	$limit = (int) $limit;

	if (trim($str) === '' OR UTF8::strlen($str) <= $limit)
		return $str;

	if ($limit <= 0)
		return $end_char;

	if ($preserve_words == FALSE)
	{
		return rtrim(UTF8::substr($str, 0, $limit)).$end_char;
	}

	preg_match('/^.{'.($limit - 1).'}\S*/us', $str, $matches);

	return rtrim($matches[0]).(strlen($matches[0]) == strlen($str) ? '' : $end_char);
}

public static alternate ( )

Alternates between two or more strings.

Returns:
  • string
Source:
public static function alternate()
{
	static $i;

	if (func_num_args() === 0)
	{
		$i = 0;
		return '';
	}

	$args = func_get_args();
	return $args[($i++ % count($args))];
}

public static random ( string $type = 'alnum', integer $length = 8 )

Generates a random string of a given type and length.

Returns:
  • string
Source:
public static function random($type = 'alnum', $length = 8)
{
	$utf8 = FALSE;

	switch ($type)
	{
		case 'alnum':
			$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
		break;
		case 'alpha':
			$pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
		break;
		case 'hexdec':
			$pool = '0123456789abcdef';
		break;
		case 'numeric':
			$pool = '0123456789';
		break;
		case 'nozero':
			$pool = '123456789';
		break;
		case 'distinct':
			$pool = '2345679ACDEFHJKLMNPRSTUVWXYZ';
		break;
		default:
			$pool = (string) $type;
			$utf8 = ! UTF8::is_ascii($pool);
		break;
	}

	// Split the pool into an array of characters
	$pool = ($utf8 === TRUE) ? UTF8::str_split($pool, 1) : str_split($pool, 1);

	// Largest pool key
	$max = count($pool) - 1;

	$str = '';
	for ($i = 0; $i < $length; $i++)
	{
		// Select a random character from the pool and add it to the string
		$str .= $pool[mt_rand(0, $max)];
	}

	// Make sure alnum strings contain at least one letter and one digit
	if ($type === 'alnum' AND $length > 1)
	{
		if (ctype_alpha($str))
		{
			// Add a random digit
			$str[mt_rand(0, $length - 1)] = chr(mt_rand(48, 57));
		}
		elseif (ctype_digit($str))
		{
			// Add a random letter
			$str[mt_rand(0, $length - 1)] = chr(mt_rand(65, 90));
		}
	}

	return $str;
}
tutorial
alnum alpha-numeric characters
alpha alphabetical characters
hexdec hexadecimal characters, 0-9 plus a-f
numeric digit characters, 0-9
nozero digit characters, 1-9
distinct clearly distinct alpha-numeric characters

public static reduce_slashes ( string $str )

Reduces multiple slashes in a string to single slashes.

Returns:
  • string
Source:
public static function reduce_slashes($str)
{
	return preg_replace('#(?

public static censor ( string $str, array $badwords, string $replacement = '#', boolean $replace_partial_words = true )

Replaces the given words with a string.

Returns:
  • string
Source:
public static function censor($str, $badwords, $replacement = '#', $replace_partial_words = TRUE)
{
	foreach ((array) $badwords as $key => $badword)
	{
		$badwords[$key] = str_replace('\*', '\S*?', preg_quote((string) $badword));
	}

	$regex = '('.implode('|', $badwords).')';

	if ($replace_partial_words === FALSE)
	{
		// Just using \b isn't sufficient when we need to replace a badword that already contains word boundaries itself
		$regex = '(?<=\b|\s|^)'.$regex.'(?=\b|\s|$)';
	}

	$regex = '!'.$regex.'!ui';

	if (UTF8::strlen($replacement) == 1)
	{
		$regex .= 'e';
		return preg_replace($regex, 'str_repeat($replacement, UTF8::strlen(\'$1\'))', $str);
	}

	return preg_replace($regex, $replacement, $str);
}

public static similar ( array $words )

Finds the text that is similar between a set of words.

Returns:
  • string
Source:
public static function similar(array $words)
{
	// First word is the word to match against
	$word = current($words);

	for ($i = 0, $max = strlen($word); $i < $max; ++$i)
	{
		foreach ($words as $w)
		{
			// Once a difference is found, break out of the loops
			if ( ! isset($w[$i]) OR $w[$i] !== $word[$i])
				break 2;
		}
	}

	// Return the similar text
	return substr($word, 0, $i);
}

Converts text email addresses and anchors into links.

Returns:
  • string
Source:
public static function auto_link($text)
{
	// Auto link emails first to prevent problems with "www.domain.com@example.com"
	return Text::auto_link_urls(Text::auto_link_emails($text));
}

Converts text anchors into links.

Returns:
  • string
Source:
public static function auto_link_urls($text)
{
	// Finds all http/https/ftp/ftps links that are not part of an existing html anchor
	if (preg_match_all('~\b(?)(?:ht|f)tps?://\S+(?:/|\b)~i', $text, $matches))
	{
		foreach ($matches[0] as $match)
		{
			// Replace each link with an anchor
			$text = str_replace($match, HTML::anchor($match), $text);
		}
	}

	// Find all naked www.links.com (without http://)
	if (preg_match_all('~\b(?

Converts text email addresses into links.

Returns:
  • string
Source:
public static function auto_link_emails($text)
{
	// Finds all email addresses that are not part of an existing html mailto anchor
	// Note: The "58;" negative lookbehind prevents matching of existing encoded html mailto anchors
	//       The html entity for a colon (:) is : or : or : etc.
	if (preg_match_all('~\b(?|58;)(?!\.)[-+_a-z0-9.]++(?

public static auto_p ( string $str, boolean $br = true )

Automatically applies

<

p> and
markup to text. Basically nl2br() on steroids.

Returns:
  • string
Source:
public static function auto_p($str, $br = TRUE)
{
	// Trim whitespace
	if (($str = trim($str)) === '')
		return '';

	// Standardize newlines
	$str = str_replace(array("\r\n", "\r"), "\n", $str);

	// Trim whitespace on each line
	$str = preg_replace('~^[ \t]+~m', '', $str);
	$str = preg_replace('~[ \t]+$~m', '', $str);

	// The following regexes only need to be executed if the string contains html
	if ($html_found = (strpos($str, '<') !== FALSE))
	{
		// Elements that should not be surrounded by p tags
		$no_p = '(?:p|div|h[1-6r]|ul|ol|li|blockquote|d[dlt]|pre|t[dhr]|t(?:able|body|foot|head)|c(?:aption|olgroup)|form|s(?:elect|tyle)|a(?:ddress|rea)|ma(?:p|th))';

		// Put at least two linebreaks before and after $no_p elements
		$str = preg_replace('~^<'.$no_p.'[^>]*+>~im', "\n$0", $str);
		$str = preg_replace('~$~im', "$0\n", $str);
	}

	// Do the 

magic! $str = '

'.trim($str).'

'; $str = preg_replace('~\n{2,}~', "

\n\n

", $str); // The following regexes only need to be executed if the string contains html if ($html_found !== FALSE) { // Remove p tags around $no_p elements $str = preg_replace('~

(?=]*+>)~i', '', $str); $str = preg_replace('~(]*+>)

~i', '$1', $str); } // Convert single linebreaks to
if ($br === TRUE) { $str = preg_replace('~(?\n", $str); } return $str; }

public static bytes ( integer $bytes, string $force_unit = NULL, string $format = NULL, boolean $si = true )

Returns human readable sizes.

Returns:
  • string
Source:
public static function bytes($bytes, $force_unit = NULL, $format = NULL, $si = TRUE)
{
	// Format string
	$format = ($format === NULL) ? '%01.2f %s' : (string) $format;

	// IEC prefixes (binary)
	if ($si == FALSE OR strpos($force_unit, 'i') !== FALSE)
	{
		$units = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB');
		$mod   = 1024;
	}
	// SI prefixes (decimal)
	else
	{
		$units = array('B', 'kB', 'MB', 'GB', 'TB', 'PB');
		$mod   = 1000;
	}

	// Determine unit to use
	if (($power = array_search((string) $force_unit, $units)) === FALSE)
	{
		$power = ($bytes > 0) ? floor(log($bytes, $mod)) : 0;
	}

	return sprintf($format, $bytes / pow($mod, $power), $units[$power]);
}
see
Based on original functions written by:
Aidan Lister: http://aidanlister.com/repos/v/function.size_readable.php
Quentin Zervaas: http://www.phpriot.com/d/code/strings/filesize-format/

public static widont ( string $str )

Prevents widow words by inserting a non-breaking space between the last two words.

Returns:
  • string
Source:
public static function widont($str)
{
	$str = rtrim($str);
	$space = strrpos($str, ' ');

	if ($space !== FALSE)
	{
		$str = substr($str, 0, $space).' '.substr($str, $space + 1);
	}

	return $str;
}
see
http://www.shauninman.com/archive/2006/08/22/widont_wordpress_plugin