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; } /** * Reduces multiple slashes in a string to single slashes. * * @param string string to reduce slashes of * @return string */ public static function reduce_slashes($str) { return preg_replace('#(? $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 (mb_strlen($replacement) == 1) { $regex .= 'e'; return preg_replace($regex, 'str_repeat($replacement, mb_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); } /** * An alternative to the php levenshtein() function that work out the * distance between 2 words using the Damerau–Levenshtein algorithm. * Credit: http://forums.devnetwork.net/viewtopic.php?f=50&t=89094 * * @see http://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance * @param string first word * @param string second word * @return int distance between words */ public static function distance($string1, $string2) { $string1_length = strlen($string1); $string2_length = strlen($string2); // Here we start building the table of values $matrix = array(); // String1 length + 1 = rows. for ($i = 0; $i <= $string1_length; ++$i) { $matrix[$i][0] = $i; } // String2 length + 1 columns. for ($j = 0; $j <= $string2_length; ++$j) { $matrix[0][$j] = $j; } for ($i = 1; $i <= $string1_length; ++$i) { for ($j = 1; $j <= $string2_length; ++$j) { $cost = substr($string1, $i - 1, 1) == substr($string2, $j - 1, 1) ? 0 : 1; $matrix[$i][$j] = min( $matrix[$i - 1][$j] + 1, // deletion $matrix[$i][$j - 1] + 1, // insertion $matrix[$i - 1][$j - 1] + $cost // substitution ); if ($i > 1 && $j > 1 && (substr($string1, $i - 1, 1) == substr($string2, $j - 2, 1)) && (substr($string1, $i - 2, 1) == substr($string2, $j - 1, 1))) { $matrix[$i][$j] = min( $matrix[$i][$j], $matrix[$i - 2][$j - 2] + $cost // transposition ); } } } return $matrix[$string1_length][$string2_length]; } /** * Converts text anchors into links. * * @param string text to auto link * @return string */ public static function auto_link_urls($text) { $regex = '~\\b' .'((?:ht|f)tps?://)?' // protocol .'(?:[-a-zA-Z0-9]{1,63}\.)+' // host name .'(?:[0-9]{1,3}|aero|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)' // tlds .'(?:/[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]*?)?' // path .'(?:\?[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]+?)?' // query .'(?:#[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]+?)?' // fragment .'(?=[?.!,;:"]?(?:\s|$))~'; // punctuation and url end $result = ""; $position = 0; while (preg_match($regex, $text, $match, PREG_OFFSET_CAPTURE, $position)) { list($url, $url_pos) = $match[0]; // Add the text before the url $result .= substr($text, $position, $url_pos - $position); // Default to http:// $full_url = empty($match[1][0]) ? 'http://'.$url : $url; // Add the hyperlink. $result .= html::anchor($full_url, $url); // New position to start parsing $position = $url_pos + strlen($url); } return $result.substr($text, $position); } /** * Converts text email addresses into links. * * @param string text to auto link * @return string */ 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.]++(? and
markup to text. Basically nl2br() on steroids. * * @param string subject * @param boolean convert single linebreaks to
* @return string */ 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; } /** * 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; } /** * Tests whether a string contains only 7bit ASCII bytes. This is used to * determine when to use native functions or UTF-8 functions. * * @see http://sourceforge.net/projects/phputf8/ * @copyright (c) 2007-2009 Kohana Team * @copyright (c) 2005 Harry Fuecks * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt * * @param string string to check * @return bool */ public static function is_ascii($str) { return is_string($str) AND ! preg_match('/[^\x00-\x7F]/S', $str); } /** * Strips out device control codes in the ASCII range. * * @see http://sourceforge.net/projects/phputf8/ * @copyright (c) 2007-2009 Kohana Team * @copyright (c) 2005 Harry Fuecks * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt * * @param string string to clean * @return string */ public static function strip_ascii_ctrl($str) { return preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S', '', $str); } /** * Strips out all non-7bit ASCII bytes. * * @see http://sourceforge.net/projects/phputf8/ * @copyright (c) 2007-2009 Kohana Team * @copyright (c) 2005 Harry Fuecks * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt * * @param string string to clean * @return string */ public static function strip_non_ascii($str) { return preg_replace('/[^\x00-\x7F]+/S', '', $str); } /** * Replaces special/accented UTF-8 characters by ASCII-7 'equivalents'. * * @author Andreas Gohr * @see http://sourceforge.net/projects/phputf8/ * @copyright (c) 2007-2009 Kohana Team * @copyright (c) 2005 Harry Fuecks * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt * * @param string string to transliterate * @param integer -1 lowercase only, +1 uppercase only, 0 both cases * @return string */ public static function transliterate_to_ascii($str, $case = 0) { static $UTF8_LOWER_ACCENTS = NULL; static $UTF8_UPPER_ACCENTS = NULL; if ($case <= 0) { if ($UTF8_LOWER_ACCENTS === NULL) { $UTF8_LOWER_ACCENTS = array( 'à' => 'a', 'ô' => 'o', 'ď' => 'd', 'ḟ' => 'f', 'ë' => 'e', 'š' => 's', 'ơ' => 'o', 'ß' => 'ss', 'ă' => 'a', 'ř' => 'r', 'ț' => 't', 'ň' => 'n', 'ā' => 'a', 'ķ' => 'k', 'ŝ' => 's', 'ỳ' => 'y', 'ņ' => 'n', 'ĺ' => 'l', 'ħ' => 'h', 'ṗ' => 'p', 'ó' => 'o', 'ú' => 'u', 'ě' => 'e', 'é' => 'e', 'ç' => 'c', 'ẁ' => 'w', 'ċ' => 'c', 'õ' => 'o', 'ṡ' => 's', 'ø' => 'o', 'ģ' => 'g', 'ŧ' => 't', 'ș' => 's', 'ė' => 'e', 'ĉ' => 'c', 'ś' => 's', 'î' => 'i', 'ű' => 'u', 'ć' => 'c', 'ę' => 'e', 'ŵ' => 'w', 'ṫ' => 't', 'ū' => 'u', 'č' => 'c', 'ö' => 'o', 'è' => 'e', 'ŷ' => 'y', 'ą' => 'a', 'ł' => 'l', 'ų' => 'u', 'ů' => 'u', 'ş' => 's', 'ğ' => 'g', 'ļ' => 'l', 'ƒ' => 'f', 'ž' => 'z', 'ẃ' => 'w', 'ḃ' => 'b', 'å' => 'a', 'ì' => 'i', 'ï' => 'i', 'ḋ' => 'd', 'ť' => 't', 'ŗ' => 'r', 'ä' => 'a', 'í' => 'i', 'ŕ' => 'r', 'ê' => 'e', 'ü' => 'u', 'ò' => 'o', 'ē' => 'e', 'ñ' => 'n', 'ń' => 'n', 'ĥ' => 'h', 'ĝ' => 'g', 'đ' => 'd', 'ĵ' => 'j', 'ÿ' => 'y', 'ũ' => 'u', 'ŭ' => 'u', 'ư' => 'u', 'ţ' => 't', 'ý' => 'y', 'ő' => 'o', 'â' => 'a', 'ľ' => 'l', 'ẅ' => 'w', 'ż' => 'z', 'ī' => 'i', 'ã' => 'a', 'ġ' => 'g', 'ṁ' => 'm', 'ō' => 'o', 'ĩ' => 'i', 'ù' => 'u', 'į' => 'i', 'ź' => 'z', 'á' => 'a', 'û' => 'u', 'þ' => 'th', 'ð' => 'dh', 'æ' => 'ae', 'µ' => 'u', 'ĕ' => 'e', 'ı' => 'i', ); } $str = str_replace( array_keys($UTF8_LOWER_ACCENTS), array_values($UTF8_LOWER_ACCENTS), $str ); } if ($case >= 0) { if ($UTF8_UPPER_ACCENTS === NULL) { $UTF8_UPPER_ACCENTS = array( 'À' => 'A', 'Ô' => 'O', 'Ď' => 'D', 'Ḟ' => 'F', 'Ë' => 'E', 'Š' => 'S', 'Ơ' => 'O', 'Ă' => 'A', 'Ř' => 'R', 'Ț' => 'T', 'Ň' => 'N', 'Ā' => 'A', 'Ķ' => 'K', 'Ĕ' => 'E', 'Ŝ' => 'S', 'Ỳ' => 'Y', 'Ņ' => 'N', 'Ĺ' => 'L', 'Ħ' => 'H', 'Ṗ' => 'P', 'Ó' => 'O', 'Ú' => 'U', 'Ě' => 'E', 'É' => 'E', 'Ç' => 'C', 'Ẁ' => 'W', 'Ċ' => 'C', 'Õ' => 'O', 'Ṡ' => 'S', 'Ø' => 'O', 'Ģ' => 'G', 'Ŧ' => 'T', 'Ș' => 'S', 'Ė' => 'E', 'Ĉ' => 'C', 'Ś' => 'S', 'Î' => 'I', 'Ű' => 'U', 'Ć' => 'C', 'Ę' => 'E', 'Ŵ' => 'W', 'Ṫ' => 'T', 'Ū' => 'U', 'Č' => 'C', 'Ö' => 'O', 'È' => 'E', 'Ŷ' => 'Y', 'Ą' => 'A', 'Ł' => 'L', 'Ų' => 'U', 'Ů' => 'U', 'Ş' => 'S', 'Ğ' => 'G', 'Ļ' => 'L', 'Ƒ' => 'F', 'Ž' => 'Z', 'Ẃ' => 'W', 'Ḃ' => 'B', 'Å' => 'A', 'Ì' => 'I', 'Ï' => 'I', 'Ḋ' => 'D', 'Ť' => 'T', 'Ŗ' => 'R', 'Ä' => 'A', 'Í' => 'I', 'Ŕ' => 'R', 'Ê' => 'E', 'Ü' => 'U', 'Ò' => 'O', 'Ē' => 'E', 'Ñ' => 'N', 'Ń' => 'N', 'Ĥ' => 'H', 'Ĝ' => 'G', 'Đ' => 'D', 'Ĵ' => 'J', 'Ÿ' => 'Y', 'Ũ' => 'U', 'Ŭ' => 'U', 'Ư' => 'U', 'Ţ' => 'T', 'Ý' => 'Y', 'Ő' => 'O', 'Â' => 'A', 'Ľ' => 'L', 'Ẅ' => 'W', 'Ż' => 'Z', 'Ī' => 'I', 'Ã' => 'A', 'Ġ' => 'G', 'Ṁ' => 'M', 'Ō' => 'O', 'Ĩ' => 'I', 'Ù' => 'U', 'Į' => 'I', 'Ź' => 'Z', 'Á' => 'A', 'Û' => 'U', 'Þ' => 'Th', 'Ð' => 'Dh', 'Æ' => 'Ae', 'İ' => 'I', ); } $str = str_replace( array_keys($UTF8_UPPER_ACCENTS), array_values($UTF8_UPPER_ACCENTS), $str ); } return $str; } } // End text