Linux multi-monitor fullscreen support
[ryzomcore.git] / web / public_php / webtt / cake / libs / string.php
blobecb63f35237a1a1bcd4c517ddf182b136b9a32c1
1 <?php
2 /**
3 * String handling methods.
5 * PHP versions 4 and 5
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
15 * @package cake
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)
21 /**
22 * String handling methods.
25 * @package cake
26 * @subpackage cake.cake.libs
28 class String {
30 /**
31 * Generate a random UUID
33 * @see http://www.ietf.org/rfc/rfc4122.txt
34 * @return RFC 4122 UUID
35 * @static
37 function uuid() {
38 $node = env('SERVER_ADDR');
39 $pid = null;
41 if (strpos($node, ':') !== false) {
42 if (substr_count($node, '::')) {
43 $node = str_replace(
44 '::', str_repeat(':0000', 8 - substr_count($node, ':')) . ':', $node
47 $node = explode(':', $node) ;
48 $ipv6 = '' ;
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) {
56 $node = null;
57 } else {
58 $node = crc32($node);
60 } elseif (empty($node)) {
61 $host = env('HOSTNAME');
63 if (empty($host)) {
64 $host = env('HOST');
67 if (!empty($host)) {
68 $ip = gethostbyname($host);
70 if ($ip === $host) {
71 $node = crc32($host);
72 } else {
73 $node = ip2long($ip);
76 } elseif ($node !== '127.0.0.1') {
77 $node = ip2long($node);
78 } else {
79 $node = null;
82 if (empty($node)) {
83 $node = crc32(Configure::read('Security.salt'));
86 if (function_exists('zend_thread_id')) {
87 $pid = zend_thread_id();
88 } else {
89 $pid = getmypid();
92 if (!$pid || $pid > 65535) {
93 $pid = mt_rand(0, 0xfff) | 0x4000;
96 list($timeMid, $timeLow) = explode(' ', microtime());
97 $uuid = sprintf(
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
102 return $uuid;
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.
114 * @access public
115 * @static
117 function tokenize($data, $separator = ',', $leftBound = '(', $rightBound = ')') {
118 if (empty($data) || is_array($data)) {
119 return $data;
122 $depth = 0;
123 $offset = 0;
124 $buffer = '';
125 $results = array();
126 $length = strlen($data);
127 $open = false;
129 while ($offset <= $length) {
130 $tmpOffset = -1;
131 $offsets = array(
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;
145 $buffer = '';
146 } else {
147 $buffer .= $data{$tmpOffset};
149 if ($leftBound != $rightBound) {
150 if ($data{$tmpOffset} == $leftBound) {
151 $depth++;
153 if ($data{$tmpOffset} == $rightBound) {
154 $depth--;
156 } else {
157 if ($data{$tmpOffset} == $leftBound) {
158 if (!$open) {
159 $depth++;
160 $open = true;
161 } else {
162 $depth--;
163 $open = false;
167 $offset = ++$tmpOffset;
168 } else {
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);
179 } else {
180 $data = array();
182 return $data;
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
204 * @return string
205 * @access public
206 * @static
208 function insert($str, $data, $options = array()) {
209 $defaults = array(
210 'before' => ':', 'after' => null, 'escape' => '\\', 'format' => null, 'clean' => false
212 $options += $defaults;
213 $format = $options['format'];
214 $data = (array)$data;
215 if (empty($data)) {
216 return ($options['clean']) ? String::cleanInsert($str, $options) : $str;
219 if (!isset($format)) {
220 $format = sprintf(
221 '/(?<!%s)%s%%s%s/',
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))) {
229 $offset = 0;
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;
236 } else {
237 asort($data);
239 $hashKeys = array();
240 foreach ($data as $key => $value) {
241 $hashKeys[] = crc32($key);
244 $tempData = array_combine(array_keys($data), array_values($hashKeys));
245 krsort($tempData);
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().
269 * @param string $str
270 * @param string $options
271 * @return string
272 * @access public
273 * @static
274 * @see String::insert()
276 function cleanInsert($str, $options) {
277 $clean = $options['clean'];
278 if (!$clean) {
279 return $str;
281 if ($clean === true) {
282 $clean = array('method' => 'text');
284 if (!is_array($clean)) {
285 $clean = array('method' => $options['clean']);
287 switch ($clean['method']) {
288 case 'html':
289 $clean = array_merge(array(
290 'word' => '[\w,.]+',
291 'andText' => true,
292 'replacement' => '',
293 ), $clean);
294 $kleenex = sprintf(
295 '/[\s]*[a-z]+=(")(%s%s%s[\s]*)+\\1/i',
296 preg_quote($options['before'], '/'),
297 $clean['word'],
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);
305 break;
306 case 'text':
307 $clean = array_merge(array(
308 'word' => '[\w,.]+',
309 'gap' => '[\s]*(?:(?:and|or)[\s]*)?',
310 'replacement' => '',
311 ), $clean);
313 $kleenex = sprintf(
314 '/(%s%s%s%s|%s%s%s%s)/',
315 preg_quote($options['before'], '/'),
316 $clean['word'],
317 preg_quote($options['after'], '/'),
318 $clean['gap'],
319 $clean['gap'],
320 preg_quote($options['before'], '/'),
321 $clean['word'],
322 preg_quote($options['after'], '/')
324 $str = preg_replace($kleenex, $clean['replacement'], $str);
325 break;
327 return $str;