2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* String handling methods.
|
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-05-01 21:05:46 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.libs
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.5551
|
2009-05-01 21:05:46 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* String handling methods.
|
|
|
|
*
|
|
|
|
*
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.libs
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-09-30 04:01:47 +00:00
|
|
|
class String {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Generate a random UUID
|
|
|
|
*
|
|
|
|
* @see http://www.ietf.org/rfc/rfc4122.txt
|
|
|
|
* @return RFC 4122 UUID
|
|
|
|
*/
|
2010-04-14 04:29:44 +00:00
|
|
|
public static function uuid() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$node = env('SERVER_ADDR');
|
|
|
|
$pid = null;
|
|
|
|
|
|
|
|
if (strpos($node, ':') !== false) {
|
|
|
|
if (substr_count($node, '::')) {
|
2009-05-01 21:05:46 +00:00
|
|
|
$node = str_replace(
|
|
|
|
'::', str_repeat(':0000', 8 - substr_count($node, ':')) . ':', $node
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$node = explode(':', $node) ;
|
|
|
|
$ipv6 = '' ;
|
|
|
|
|
|
|
|
foreach ($node as $id) {
|
|
|
|
$ipv6 .= str_pad(base_convert($id, 16, 2), 16, 0, STR_PAD_LEFT);
|
|
|
|
}
|
|
|
|
$node = base_convert($ipv6, 2, 10);
|
|
|
|
|
|
|
|
if (strlen($node) < 38) {
|
|
|
|
$node = null;
|
|
|
|
} else {
|
|
|
|
$node = crc32($node);
|
|
|
|
}
|
|
|
|
} elseif (empty($node)) {
|
|
|
|
$host = env('HOSTNAME');
|
|
|
|
|
|
|
|
if (empty($host)) {
|
|
|
|
$host = env('HOST');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($host)) {
|
|
|
|
$ip = gethostbyname($host);
|
|
|
|
|
|
|
|
if ($ip === $host) {
|
|
|
|
$node = crc32($host);
|
|
|
|
} else {
|
|
|
|
$node = ip2long($ip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif ($node !== '127.0.0.1') {
|
|
|
|
$node = ip2long($node);
|
|
|
|
} else {
|
|
|
|
$node = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($node)) {
|
|
|
|
$node = crc32(Configure::read('Security.salt'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (function_exists('zend_thread_id')) {
|
|
|
|
$pid = zend_thread_id();
|
|
|
|
} else {
|
|
|
|
$pid = getmypid();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$pid || $pid > 65535) {
|
|
|
|
$pid = mt_rand(0, 0xfff) | 0x4000;
|
|
|
|
}
|
|
|
|
|
|
|
|
list($timeMid, $timeLow) = explode(' ', microtime());
|
2009-05-01 21:05:46 +00:00
|
|
|
$uuid = sprintf(
|
|
|
|
"%08x-%04x-%04x-%02x%02x-%04x%08x", (int)$timeLow, (int)substr($timeMid, 2) & 0xffff,
|
|
|
|
mt_rand(0, 0xfff) | 0x4000, mt_rand(0, 0x3f) | 0x80, mt_rand(0, 0xff), $pid, $node
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
return $uuid;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-05-01 21:05:46 +00:00
|
|
|
* Tokenizes a string using $separator, ignoring any instance of $separator that appears between
|
|
|
|
* $leftBound and $rightBound
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $data The data to tokenize
|
2010-01-25 20:37:55 +00:00
|
|
|
* @param string $separator The token to split the data on.
|
|
|
|
* @param string $leftBound The left boundary to ignore separators in.
|
|
|
|
* @param string $rightBound The right boundary to ignore separators in.
|
|
|
|
* @return array Array of tokens in $data.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-14 04:29:44 +00:00
|
|
|
public static function tokenize($data, $separator = ',', $leftBound = '(', $rightBound = ')') {
|
2008-10-23 00:10:44 +00:00
|
|
|
if (empty($data) || is_array($data)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
$depth = 0;
|
|
|
|
$offset = 0;
|
|
|
|
$buffer = '';
|
|
|
|
$results = array();
|
|
|
|
$length = strlen($data);
|
|
|
|
$open = false;
|
|
|
|
|
|
|
|
while ($offset <= $length) {
|
|
|
|
$tmpOffset = -1;
|
2009-05-01 21:05:46 +00:00
|
|
|
$offsets = array(
|
|
|
|
strpos($data, $separator, $offset),
|
|
|
|
strpos($data, $leftBound, $offset),
|
|
|
|
strpos($data, $rightBound, $offset)
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
for ($i = 0; $i < 3; $i++) {
|
|
|
|
if ($offsets[$i] !== false && ($offsets[$i] < $tmpOffset || $tmpOffset == -1)) {
|
|
|
|
$tmpOffset = $offsets[$i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($tmpOffset !== -1) {
|
|
|
|
$buffer .= substr($data, $offset, ($tmpOffset - $offset));
|
|
|
|
if ($data{$tmpOffset} == $separator && $depth == 0) {
|
|
|
|
$results[] = $buffer;
|
|
|
|
$buffer = '';
|
|
|
|
} else {
|
|
|
|
$buffer .= $data{$tmpOffset};
|
|
|
|
}
|
2008-09-12 05:11:34 +00:00
|
|
|
if ($leftBound != $rightBound) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($data{$tmpOffset} == $leftBound) {
|
|
|
|
$depth++;
|
2008-09-12 05:11:34 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($data{$tmpOffset} == $rightBound) {
|
|
|
|
$depth--;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($data{$tmpOffset} == $leftBound) {
|
|
|
|
if (!$open) {
|
|
|
|
$depth++;
|
|
|
|
$open = true;
|
|
|
|
} else {
|
|
|
|
$depth--;
|
|
|
|
$open = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$offset = ++$tmpOffset;
|
|
|
|
} else {
|
|
|
|
$results[] = $buffer . substr($data, $offset);
|
|
|
|
$offset = $length + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($results) && !empty($buffer)) {
|
|
|
|
$results[] = $buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($results)) {
|
|
|
|
$data = array_map('trim', $results);
|
|
|
|
} else {
|
|
|
|
$data = array();
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-05-01 21:05:46 +00:00
|
|
|
* Replaces variable placeholders inside a $str with any given $data. Each key in the $data array
|
|
|
|
* corresponds to a variable placeholder name in $str.
|
2009-10-13 16:09:06 +00:00
|
|
|
* Example: `String::insert(':name is :age years old.', array('name' => 'Bob', '65'));`
|
2009-05-01 21:05:46 +00:00
|
|
|
* Returns: Bob is 65 years old.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Available $options are:
|
2009-10-13 16:09:06 +00:00
|
|
|
*
|
|
|
|
* - before: The character or string in front of the name of the variable placeholder (Defaults to `:`)
|
|
|
|
* - after: The character or string after the name of the variable placeholder (Defaults to null)
|
|
|
|
* - escape: The character or string used to escape the before character / string (Defaults to `\`)
|
|
|
|
* - format: A regex to use for matching variable placeholders. Default is: `/(?<!\\)\:%s/`
|
|
|
|
* (Overwrites before, after, breaks escape / clean)
|
|
|
|
* - clean: A boolean or array with instructions for String::cleanInsert
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $str A string containing variable placeholders
|
2009-05-01 21:05:46 +00:00
|
|
|
* @param string $data A key => val array where each key stands for a placeholder variable name
|
2009-10-13 16:09:06 +00:00
|
|
|
* to be replaced with val
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $options An array of options, see description above
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-04-14 04:29:44 +00:00
|
|
|
public static function insert($str, $data, $options = array()) {
|
2009-03-09 20:03:02 +00:00
|
|
|
$defaults = array(
|
|
|
|
'before' => ':', 'after' => null, 'escape' => '\\', 'format' => null, 'clean' => false
|
2008-05-31 12:36:38 +00:00
|
|
|
);
|
2009-03-09 20:03:02 +00:00
|
|
|
$options += $defaults;
|
2008-05-30 11:40:08 +00:00
|
|
|
$format = $options['format'];
|
2009-10-07 02:02:02 +00:00
|
|
|
$data = (array)$data;
|
2009-11-03 23:21:01 +00:00
|
|
|
if (empty($data)) {
|
|
|
|
return ($options['clean']) ? String::cleanInsert($str, $options) : $str;
|
|
|
|
}
|
2008-05-31 12:36:38 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!isset($format)) {
|
|
|
|
$format = sprintf(
|
|
|
|
'/(?<!%s)%s%%s%s/',
|
|
|
|
preg_quote($options['escape'], '/'),
|
|
|
|
str_replace('%', '%%', preg_quote($options['before'], '/')),
|
|
|
|
str_replace('%', '%%', preg_quote($options['after'], '/'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2009-10-13 15:15:17 +00:00
|
|
|
if (strpos($str, '?') !== false && is_numeric(key($data))) {
|
2008-05-31 12:36:38 +00:00
|
|
|
$offset = 0;
|
2009-03-09 20:03:02 +00:00
|
|
|
while (($pos = strpos($str, '?', $offset)) !== false) {
|
2008-05-31 12:36:38 +00:00
|
|
|
$val = array_shift($data);
|
2008-07-03 02:19:19 +00:00
|
|
|
$offset = $pos + strlen($val);
|
2008-05-31 12:36:38 +00:00
|
|
|
$str = substr_replace($str, $val, $pos, 1);
|
|
|
|
}
|
2009-10-07 02:02:02 +00:00
|
|
|
return ($options['clean']) ? String::cleanInsert($str, $options) : $str;
|
2008-05-31 12:36:38 +00:00
|
|
|
} else {
|
2009-04-09 13:15:43 +00:00
|
|
|
asort($data);
|
|
|
|
|
2009-10-07 02:02:02 +00:00
|
|
|
$hashKeys = array();
|
|
|
|
foreach ($data as $key => $value) {
|
|
|
|
$hashKeys[] = crc32($key);
|
|
|
|
}
|
|
|
|
|
2008-07-07 23:24:39 +00:00
|
|
|
$tempData = array_combine(array_keys($data), array_values($hashKeys));
|
2010-08-03 08:10:14 +00:00
|
|
|
krsort($tempData);
|
2008-09-12 05:11:34 +00:00
|
|
|
foreach ($tempData as $key => $hashVal) {
|
2008-05-31 12:36:38 +00:00
|
|
|
$key = sprintf($format, preg_quote($key, '/'));
|
2008-07-07 23:24:39 +00:00
|
|
|
$str = preg_replace($key, $hashVal, $str);
|
2008-05-31 12:36:38 +00:00
|
|
|
}
|
2008-07-07 23:24:39 +00:00
|
|
|
$dataReplacements = array_combine($hashKeys, array_values($data));
|
2009-10-07 02:02:02 +00:00
|
|
|
foreach ($dataReplacements as $tmpHash => $tmpValue) {
|
|
|
|
$tmpValue = (is_array($tmpValue)) ? '' : $tmpValue;
|
|
|
|
$str = str_replace($tmpHash, $tmpValue, $str);
|
2008-09-12 05:11:34 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-05-31 12:36:38 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!isset($options['format']) && isset($options['before'])) {
|
|
|
|
$str = str_replace($options['escape'].$options['before'], $options['before'], $str);
|
|
|
|
}
|
2009-10-07 02:02:02 +00:00
|
|
|
return ($options['clean']) ? String::cleanInsert($str, $options) : $str;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-03-28 16:53:57 +00:00
|
|
|
* Cleans up a String::insert() formated string with given $options depending on the 'clean' key in
|
2009-05-01 21:05:46 +00:00
|
|
|
* $options. The default method used is text but html is also available. The goal of this function
|
|
|
|
* is to replace all whitespace and uneeded markup around placeholders that did not get replaced
|
2010-03-28 16:53:57 +00:00
|
|
|
* by String::insert().
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2008-09-12 05:11:34 +00:00
|
|
|
* @param string $str
|
|
|
|
* @param string $options
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return string
|
2010-03-28 16:53:57 +00:00
|
|
|
* @see String::insert()
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-14 04:29:44 +00:00
|
|
|
public static function cleanInsert($str, $options) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$clean = $options['clean'];
|
|
|
|
if (!$clean) {
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
if ($clean === true) {
|
|
|
|
$clean = array('method' => 'text');
|
|
|
|
}
|
|
|
|
if (!is_array($clean)) {
|
|
|
|
$clean = array('method' => $options['clean']);
|
|
|
|
}
|
|
|
|
switch ($clean['method']) {
|
|
|
|
case 'html':
|
2008-07-31 00:16:16 +00:00
|
|
|
$clean = array_merge(array(
|
2008-10-18 23:56:41 +00:00
|
|
|
'word' => '[\w,.]+',
|
2008-05-30 11:40:08 +00:00
|
|
|
'andText' => true,
|
|
|
|
'replacement' => '',
|
|
|
|
), $clean);
|
|
|
|
$kleenex = sprintf(
|
|
|
|
'/[\s]*[a-z]+=(")(%s%s%s[\s]*)+\\1/i',
|
|
|
|
preg_quote($options['before'], '/'),
|
|
|
|
$clean['word'],
|
|
|
|
preg_quote($options['after'], '/')
|
|
|
|
);
|
|
|
|
$str = preg_replace($kleenex, $clean['replacement'], $str);
|
|
|
|
if ($clean['andText']) {
|
|
|
|
$options['clean'] = array('method' => 'text');
|
|
|
|
$str = String::cleanInsert($str, $options);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'text':
|
2008-07-31 00:16:16 +00:00
|
|
|
$clean = array_merge(array(
|
2008-10-18 23:56:41 +00:00
|
|
|
'word' => '[\w,.]+',
|
2008-05-30 11:40:08 +00:00
|
|
|
'gap' => '[\s]*(?:(?:and|or)[\s]*)?',
|
|
|
|
'replacement' => '',
|
|
|
|
), $clean);
|
|
|
|
|
|
|
|
$kleenex = sprintf(
|
|
|
|
'/(%s%s%s%s|%s%s%s%s)/',
|
|
|
|
preg_quote($options['before'], '/'),
|
|
|
|
$clean['word'],
|
|
|
|
preg_quote($options['after'], '/'),
|
|
|
|
$clean['gap'],
|
|
|
|
$clean['gap'],
|
|
|
|
preg_quote($options['before'], '/'),
|
|
|
|
$clean['word'],
|
|
|
|
preg_quote($options['after'], '/')
|
|
|
|
);
|
|
|
|
$str = preg_replace($kleenex, $clean['replacement'], $str);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return $str;
|
|
|
|
}
|
2010-10-15 02:32:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Wraps text to a specific width, can optionally wrap at word breaks.
|
|
|
|
*
|
|
|
|
* ### Options
|
|
|
|
*
|
|
|
|
* - `width` The width to wrap to. Defaults to 72
|
|
|
|
* - `wordWrap` Only wrap on words breaks (spaces) Defaults to true.
|
2010-10-16 02:46:03 +00:00
|
|
|
* - `indent` String to indent with. Defaults to null.
|
|
|
|
* - `indentAt` 0 based index to start indenting at. Defaults to 0.
|
2010-10-15 02:32:56 +00:00
|
|
|
*
|
|
|
|
* @param string $text Text the text to format.
|
|
|
|
* @param mixed $options Array of options to use, or an integer to wrap the text to.
|
|
|
|
* @return string Formatted text.
|
|
|
|
*/
|
|
|
|
public static function wrap($text, $options = array()) {
|
|
|
|
if (is_numeric($options)) {
|
|
|
|
$options = array('width' => $options);
|
|
|
|
}
|
2010-10-16 02:46:03 +00:00
|
|
|
$options += array('width' => 72, 'wordWrap' => true, 'indent' => null, 'indentAt' => 0);
|
2010-10-15 02:32:56 +00:00
|
|
|
if ($options['wordWrap']) {
|
2010-10-16 02:46:03 +00:00
|
|
|
$wrapped = wordwrap($text, $options['width'], "\n");
|
|
|
|
} else {
|
|
|
|
$wrapped = trim(chunk_split($text, $options['width'] - 1, "\n"));
|
|
|
|
}
|
|
|
|
if (!empty($options['indent'])) {
|
|
|
|
$chunks = explode("\n", $wrapped);
|
|
|
|
for ($i = $options['indentAt'], $len = count($chunks); $i < $len; $i++) {
|
|
|
|
$chunks[$i] = $options['indent'] . $chunks[$i];
|
|
|
|
}
|
|
|
|
$wrapped = implode("\n", $chunks);
|
2010-10-15 02:32:56 +00:00
|
|
|
}
|
2010-10-16 02:46:03 +00:00
|
|
|
return $wrapped;
|
2010-10-15 02:32:56 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|