3 * String handling methods.
7 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
10 * Licensed under The MIT License
11 * Redistributions of files must retain the above copyright notice.
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14 * @link http://cakephp.org CakePHP(tm) Project
16 * @subpackage cake.cake.libs
17 * @since CakePHP(tm) v 1.2.0.5551
18 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
22 * String handling methods.
26 * @subpackage cake.cake.libs
31 * Generate a random UUID
33 * @see http://www.ietf.org/rfc/rfc4122.txt
34 * @return RFC 4122 UUID
38 $node = env('SERVER_ADDR');
41 if (strpos($node, ':') !== false) {
42 if (substr_count($node, '::')) {
44 '::', str_repeat(':0000', 8 - substr_count($node, ':')) . ':', $node
47 $node = explode(':', $node) ;
50 foreach ($node as $id) {
51 $ipv6 .= str_pad(base_convert($id, 16, 2), 16, 0, STR_PAD_LEFT
);
53 $node = base_convert($ipv6, 2, 10);
55 if (strlen($node) < 38) {
60 } elseif (empty($node)) {
61 $host = env('HOSTNAME');
68 $ip = gethostbyname($host);
76 } elseif ($node !== '127.0.0.1') {
77 $node = ip2long($node);
83 $node = crc32(Configure
::read('Security.salt'));
86 if (function_exists('zend_thread_id')) {
87 $pid = zend_thread_id();
92 if (!$pid ||
$pid > 65535) {
93 $pid = mt_rand(0, 0xfff) |
0x4000;
96 list($timeMid, $timeLow) = explode(' ', microtime());
98 "%08x-%04x-%04x-%02x%02x-%04x%08x", (int)$timeLow, (int)substr($timeMid, 2) & 0xffff,
99 mt_rand(0, 0xfff) |
0x4000, mt_rand(0, 0x3f) |
0x80, mt_rand(0, 0xff), $pid, $node
106 * Tokenizes a string using $separator, ignoring any instance of $separator that appears between
107 * $leftBound and $rightBound
109 * @param string $data The data to tokenize
110 * @param string $separator The token to split the data on.
111 * @param string $leftBound The left boundary to ignore separators in.
112 * @param string $rightBound The right boundary to ignore separators in.
113 * @return array Array of tokens in $data.
117 function tokenize($data, $separator = ',', $leftBound = '(', $rightBound = ')') {
118 if (empty($data) ||
is_array($data)) {
126 $length = strlen($data);
129 while ($offset <= $length) {
132 strpos($data, $separator, $offset),
133 strpos($data, $leftBound, $offset),
134 strpos($data, $rightBound, $offset)
136 for ($i = 0; $i < 3; $i++
) {
137 if ($offsets[$i] !== false && ($offsets[$i] < $tmpOffset ||
$tmpOffset == -1)) {
138 $tmpOffset = $offsets[$i];
141 if ($tmpOffset !== -1) {
142 $buffer .= substr($data, $offset, ($tmpOffset - $offset));
143 if ($data{$tmpOffset} == $separator && $depth == 0) {
144 $results[] = $buffer;
147 $buffer .= $data{$tmpOffset};
149 if ($leftBound != $rightBound) {
150 if ($data{$tmpOffset} == $leftBound) {
153 if ($data{$tmpOffset} == $rightBound) {
157 if ($data{$tmpOffset} == $leftBound) {
167 $offset = ++
$tmpOffset;
169 $results[] = $buffer . substr($data, $offset);
170 $offset = $length +
1;
173 if (empty($results) && !empty($buffer)) {
174 $results[] = $buffer;
177 if (!empty($results)) {
178 $data = array_map('trim', $results);
186 * Replaces variable placeholders inside a $str with any given $data. Each key in the $data array
187 * corresponds to a variable placeholder name in $str.
188 * Example: `String::insert(':name is :age years old.', array('name' => 'Bob', '65'));`
189 * Returns: Bob is 65 years old.
191 * Available $options are:
193 * - before: The character or string in front of the name of the variable placeholder (Defaults to `:`)
194 * - after: The character or string after the name of the variable placeholder (Defaults to null)
195 * - escape: The character or string used to escape the before character / string (Defaults to `\`)
196 * - format: A regex to use for matching variable placeholders. Default is: `/(?<!\\)\:%s/`
197 * (Overwrites before, after, breaks escape / clean)
198 * - clean: A boolean or array with instructions for String::cleanInsert
200 * @param string $str A string containing variable placeholders
201 * @param string $data A key => val array where each key stands for a placeholder variable name
202 * to be replaced with val
203 * @param string $options An array of options, see description above
208 function insert($str, $data, $options = array()) {
210 'before' => ':', 'after' => null, 'escape' => '\\', 'format' => null, 'clean' => false
212 $options +
= $defaults;
213 $format = $options['format'];
214 $data = (array)$data;
216 return ($options['clean']) ?
String::cleanInsert($str, $options) : $str;
219 if (!isset($format)) {
222 preg_quote($options['escape'], '/'),
223 str_replace('%', '%%', preg_quote($options['before'], '/')),
224 str_replace('%', '%%', preg_quote($options['after'], '/'))
228 if (strpos($str, '?') !== false && is_numeric(key($data))) {
230 while (($pos = strpos($str, '?', $offset)) !== false) {
231 $val = array_shift($data);
232 $offset = $pos +
strlen($val);
233 $str = substr_replace($str, $val, $pos, 1);
235 return ($options['clean']) ?
String::cleanInsert($str, $options) : $str;
240 foreach ($data as $key => $value) {
241 $hashKeys[] = crc32($key);
244 $tempData = array_combine(array_keys($data), array_values($hashKeys));
246 foreach ($tempData as $key => $hashVal) {
247 $key = sprintf($format, preg_quote($key, '/'));
248 $str = preg_replace($key, $hashVal, $str);
250 $dataReplacements = array_combine($hashKeys, array_values($data));
251 foreach ($dataReplacements as $tmpHash => $tmpValue) {
252 $tmpValue = (is_array($tmpValue)) ?
'' : $tmpValue;
253 $str = str_replace($tmpHash, $tmpValue, $str);
257 if (!isset($options['format']) && isset($options['before'])) {
258 $str = str_replace($options['escape'].$options['before'], $options['before'], $str);
260 return ($options['clean']) ?
String::cleanInsert($str, $options) : $str;
264 * Cleans up a String::insert() formated string with given $options depending on the 'clean' key in
265 * $options. The default method used is text but html is also available. The goal of this function
266 * is to replace all whitespace and uneeded markup around placeholders that did not get replaced
267 * by String::insert().
270 * @param string $options
274 * @see String::insert()
276 function cleanInsert($str, $options) {
277 $clean = $options['clean'];
281 if ($clean === true) {
282 $clean = array('method' => 'text');
284 if (!is_array($clean)) {
285 $clean = array('method' => $options['clean']);
287 switch ($clean['method']) {
289 $clean = array_merge(array(
295 '/[\s]*[a-z]+=(")(%s%s%s[\s]*)+\\1/i',
296 preg_quote($options['before'], '/'),
298 preg_quote($options['after'], '/')
300 $str = preg_replace($kleenex, $clean['replacement'], $str);
301 if ($clean['andText']) {
302 $options['clean'] = array('method' => 'text');
303 $str = String::cleanInsert($str, $options);
307 $clean = array_merge(array(
309 'gap' => '[\s]*(?:(?:and|or)[\s]*)?',
314 '/(%s%s%s%s|%s%s%s%s)/',
315 preg_quote($options['before'], '/'),
317 preg_quote($options['after'], '/'),
320 preg_quote($options['before'], '/'),
322 preg_quote($options['after'], '/')
324 $str = preg_replace($kleenex, $clean['replacement'], $str);