* @copyright Copyright (c) 2008-9 TypeOneError Studios (http://www.typeoneerror.com) * @license MIT ~ http://www.opensource.org/licenses/mit-license.php * @version $Id$ * @link http://typeoneerror.com/asra * @category Asra * @package Utils */ /** * A few functions for working with strings. * * @author Ben Borowski * @copyright Copyright (c) 2008-9 TypeOneError Studios (http://www.typeoneerror.com) * @license MIT ~ http://www.opensource.org/licenses/mit-license.php * @link http://typeoneerror.com/asra * @category Asra * @package Utils */ class Asra_Utils_Strings { /** * Add trailing slash if lacking. * * @param string $path String to add slash to. * @return string String with trailing slash. */ public static function addTrailingSlash($path) { if (substr($path, -1) == '/') return $path; else return $path . '/'; } /** * Returns given lower_case_and_underscored_word as a camelCased word. * hacked version to support - and . as well as _. * * @see http://cakephp.org * @param string $lower_case_and_underscored_word Word to camelize. * @param bool $capFirst If true, return "LikeThis"; false, "likeThis". * @return string Camelized word. LikeThis. */ public static function camelize($lowerCaseAndUnderscoredWord, $capFirst = true) { $replace = ereg_replace("_|-|\.", "_", $lowerCaseAndUnderscoredWord); $replace = str_replace(" ", "", ucwords(str_replace("_", " ", $replace))); if (!$capFirst) $replace = strtolower(substr($replace, 0, 1)) . substr($replace, 1); return $replace; } /** * Clean results from database. * * @param string $_value A string to clean. * @return string Cleaned string. */ public static function clean($_value) { // -- remove slashes $s = stripslashes(trim($_value)); // -- convert quotes $s = html_entity_decode($s, ENT_QUOTES); // -- urlencode utf8 encoded data $s = urlencode(utf8_decode($s)); // -- more conversion $s = iconv("ISO-8859-1", "UTF-8", $s); // -- need to change + to %2B for flash $s = str_replace('+', ' ', $s); return $s; } /** * Format string for Flash output. * * @param string $value * @return string Formatted output. */ public static function flash($value) { $value = str_replace("\r\n","
",$value); return $value; } /** * Returns a human-readable string from $lower_case_and_underscored_word, * by replacing underscores with a space, and by upper-casing the initial characters. * * @see http://cakephp.org * @param string $lower_case_and_underscored_word String to be made more readable * @return string Human-readable string */ public static function humanize($lowerCaseAndUnderscoredWord) { $replace = ucwords(str_replace("_", " ", $lowerCaseAndUnderscoredWord)); return $replace; } /** * Encodes an object as json. * * @param array $_data Data to be encoded. * @param bool $_encode Does it need to be encoded. * @return string Json-ified data as string. */ public static function json($_data, $_encode = false) { if (function_exists('json_encode')) { if (!$_encode) return json_encode($_data); else return urlencode(utf8_encode(json_encode($_data))); } // @codeCoverageIgnoreStart else { throw new Exception('`json_encode` function not available'); } // @codeCoverageIgnoreEnd } /** * Make data safe for anti sql injection. * * @param string $value * @param bool $no_quotes * @return string */ public static function makesafe($value, $no_quotes = false) { $value = trim($value); if ($value == '' || !isset($value)) return 'NULL'; if (get_magic_quotes_gpc()) $value = stripslashes($value); if (!is_numeric($value)) { $value = mysql_real_escape_string($value); if ($no_quotes !== true) $value = "'" . $value . "'"; } return $value; } /** * Get a random char key. * * @param int $length * @param string $chars * @return string */ public static function rankey($length = 32, $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPRQSTUVWXYZ0123456789") { $code = ""; $clen = strlen($chars) - 1; while (strlen($code) < $length) { $code .= $chars[mt_rand(0,$clen)]; } return $code; } /** * Shorter version of Asra_Utils_Strings::makesafe. * * @param string $string * @return string */ public static function safe($string) { return Asra_Utils_Strings::makesafe($string); } /** * Serialize output. * * @param object $_data * @return string Serialized output */ public static function serialized($_data) { // -- encode the serailzed data $s = urlencode(utf8_encode(serialize($_data))); // -- this is needed for flash to not break $s = str_replace('+', ' ', $s); return $s; } /** * Returns an underscore-syntaxed ($like_this_dear_reader) version of the $camel_cased_word. * * @link http://cakephp.org * @param string $camel_cased_word Camel-cased word to be "underscorized". * @return string Underscore-syntaxed version of the $camel_cased_word. */ public static function underscore($camelCasedWord) { $replace = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord)); return $replace; } /** * Unserialize an object. * * @param string $value * @return mixed */ public static function unserialized($value) { $value = unserialize(urldecode($value)); return $value; } /** * Validate an email address. * * @param string $input * @param bool $require * @return string|bool String if valid or invalid while not required; * bool (false) if required and invalid. */ public static function validEmail($input, $require = true) { if (eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]{2,}\.[A-Z]{2,6}$", $input)) { return $input; } else { if ($require) return false; else return ''; } } /** * Validate a hexcode. * * @param string $hex * @return string Forced to valid hexcode. */ public static function validHex($hex) { $hex = substr($hex, 0, 6); if (strlen($hex) == 3) $hex = $hex . $hex; $hex = str_pad($hex, 6, "0", STR_PAD_RIGHT); $hex = preg_replace("/[^0-9A-Fa-f+]/i", "0", $hex); return strtoupper($hex); } }