From eba717f95f586d2538007bd18da6e9b32b076c30 Mon Sep 17 00:00:00 2001
From: Bharat Mediratta 
Date: Fri, 31 Oct 2008 22:12:14 +0000
Subject: Merge over vendor code.
git-svn-id: http://gallery.svn.sourceforge.net/svnroot/gallery/trunk/eval/gx/gallery3/trunk@18408 57fcd75e-5312-0410-8df3-f5eb6fbb1595
---
 kohana/helpers/text.php | 389 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 389 insertions(+)
 create mode 100644 kohana/helpers/text.php
(limited to 'kohana/helpers/text.php')
diff --git a/kohana/helpers/text.php b/kohana/helpers/text.php
new file mode 100644
index 00000000..ea648a19
--- /dev/null
+++ b/kohana/helpers/text.php
@@ -0,0 +1,389 @@
+ $badword)
+		{
+			$badwords[$key] = str_replace('\*', '\S*?', preg_quote((string) $badword));
+		}
+
+		$regex = '('.implode('|', $badwords).')';
+
+		if ($replace_partial_words == TRUE)
+		{
+			// 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);
+	}
+
+	/**
+	 * Finds the text that is similar between a set of words.
+	 *
+	 * @param   array   words to find similar text of
+	 * @return  string
+	 */
+	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.
+	 *
+	 * @param   string   text to auto link
+	 * @return  string
+	 */
+	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.
+	 *
+	 * @param   string   text to auto link
+	 * @return  string
+	 */
+	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(?|58;)(?!\.)[-+_a-z0-9.]++(? and 
 markup to text. Basically nl2br() on steroids.
+	 *
+	 * @param   string   subject
+	 * @return  string
+	 */
+	public static function auto_p($str)
+	{
+		// 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('~'.$no_p.'\s*+>$~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('~
(?=?'.$no_p.'[^>]*+>)~i', '', $str);
+			$str = preg_replace('~(?'.$no_p.'[^>]*+>)
~i', '$1', $str);
+		}
+
+		// Convert single linebreaks to 
+		$str = preg_replace('~(?\n", $str);
+
+		return $str;
+	}
+
+	/**
+	 * Returns human readable sizes.
+	 * @see  Based on original functions written by:
+	 * @see  Aidan Lister: http://aidanlister.com/repos/v/function.size_readable.php
+	 * @see  Quentin Zervaas: http://www.phpriot.com/d/code/strings/filesize-format/
+	 *
+	 * @param   integer  size in bytes
+	 * @param   string   a definitive unit
+	 * @param   string   the return string format
+	 * @param   boolean  whether to use SI prefixes or IEC
+	 * @return  string
+	 */
+	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]);
+	}
+
+	/**
+	 * Prevents widow words by inserting a non-breaking space between the last two words.
+	 * @see  http://www.shauninman.com/archive/2006/08/22/widont_wordpress_plugin
+	 *
+	 * @param   string  string to remove widows from
+	 * @return  string
+	 */
+	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;
+	}
+
+} // End text
\ No newline at end of file
-- 
cgit v1.2.3