1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17:
18:
19: 20: 21: 22: 23: 24:
25: class String {
26:
27: 28: 29: 30: 31: 32:
33: public static function uuid() {
34: $node = env('SERVER_ADDR');
35:
36: if (strpos($node, ':') !== false) {
37: if (substr_count($node, '::')) {
38: $node = str_replace(
39: '::', str_repeat(':0000', 8 - substr_count($node, ':')) . ':', $node
40: );
41: }
42: $node = explode(':', $node);
43: $ipSix = '';
44:
45: foreach ($node as $id) {
46: $ipSix .= str_pad(base_convert($id, 16, 2), 16, 0, STR_PAD_LEFT);
47: }
48: $node = base_convert($ipSix, 2, 10);
49:
50: if (strlen($node) < 38) {
51: $node = null;
52: } else {
53: $node = crc32($node);
54: }
55: } elseif (empty($node)) {
56: $host = env('HOSTNAME');
57:
58: if (empty($host)) {
59: $host = env('HOST');
60: }
61:
62: if (!empty($host)) {
63: $ip = gethostbyname($host);
64:
65: if ($ip === $host) {
66: $node = crc32($host);
67: } else {
68: $node = ip2long($ip);
69: }
70: }
71: } elseif ($node !== '127.0.0.1') {
72: $node = ip2long($node);
73: } else {
74: $node = null;
75: }
76:
77: if (empty($node)) {
78: $node = crc32(Configure::read('Security.salt'));
79: }
80:
81: if (function_exists('hphp_get_thread_id')) {
82: $pid = hphp_get_thread_id();
83: } elseif (function_exists('zend_thread_id')) {
84: $pid = zend_thread_id();
85: } else {
86: $pid = getmypid();
87: }
88:
89: if (!$pid || $pid > 65535) {
90: $pid = mt_rand(0, 0xfff) | 0x4000;
91: }
92:
93: list($timeMid, $timeLow) = explode(' ', microtime());
94: return sprintf(
95: "%08x-%04x-%04x-%02x%02x-%04x%08x", (int)$timeLow, (int)substr($timeMid, 2) & 0xffff,
96: mt_rand(0, 0xfff) | 0x4000, mt_rand(0, 0x3f) | 0x80, mt_rand(0, 0xff), $pid, $node
97: );
98: }
99:
100: 101: 102: 103: 104: 105: 106: 107: 108: 109:
110: public static function tokenize($data, $separator = ',', $leftBound = '(', $rightBound = ')') {
111: if (empty($data) || is_array($data)) {
112: return $data;
113: }
114:
115: $depth = 0;
116: $offset = 0;
117: $buffer = '';
118: $results = array();
119: $length = strlen($data);
120: $open = false;
121:
122: while ($offset <= $length) {
123: $tmpOffset = -1;
124: $offsets = array(
125: strpos($data, $separator, $offset),
126: strpos($data, $leftBound, $offset),
127: strpos($data, $rightBound, $offset)
128: );
129: for ($i = 0; $i < 3; $i++) {
130: if ($offsets[$i] !== false && ($offsets[$i] < $tmpOffset || $tmpOffset == -1)) {
131: $tmpOffset = $offsets[$i];
132: }
133: }
134: if ($tmpOffset !== -1) {
135: $buffer .= substr($data, $offset, ($tmpOffset - $offset));
136: if (!$depth && $data{$tmpOffset} == $separator) {
137: $results[] = $buffer;
138: $buffer = '';
139: } else {
140: $buffer .= $data{$tmpOffset};
141: }
142: if ($leftBound != $rightBound) {
143: if ($data{$tmpOffset} == $leftBound) {
144: $depth++;
145: }
146: if ($data{$tmpOffset} == $rightBound) {
147: $depth--;
148: }
149: } else {
150: if ($data{$tmpOffset} == $leftBound) {
151: if (!$open) {
152: $depth++;
153: $open = true;
154: } else {
155: $depth--;
156: }
157: }
158: }
159: $offset = ++$tmpOffset;
160: } else {
161: $results[] = $buffer . substr($data, $offset);
162: $offset = $length + 1;
163: }
164: }
165: if (empty($results) && !empty($buffer)) {
166: $results[] = $buffer;
167: }
168:
169: if (!empty($results)) {
170: return array_map('trim', $results);
171: }
172:
173: return array();
174: }
175:
176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196:
197: public static function insert($str, $data, $options = array()) {
198: $defaults = array(
199: 'before' => ':', 'after' => null, 'escape' => '\\', 'format' => null, 'clean' => false
200: );
201: $options += $defaults;
202: $format = $options['format'];
203: $data = (array)$data;
204: if (empty($data)) {
205: return ($options['clean']) ? String::cleanInsert($str, $options) : $str;
206: }
207:
208: if (!isset($format)) {
209: $format = sprintf(
210: '/(?<!%s)%s%%s%s/',
211: preg_quote($options['escape'], '/'),
212: str_replace('%', '%%', preg_quote($options['before'], '/')),
213: str_replace('%', '%%', preg_quote($options['after'], '/'))
214: );
215: }
216:
217: if (strpos($str, '?') !== false && is_numeric(key($data))) {
218: $offset = 0;
219: while (($pos = strpos($str, '?', $offset)) !== false) {
220: $val = array_shift($data);
221: $offset = $pos + strlen($val);
222: $str = substr_replace($str, $val, $pos, 1);
223: }
224: return ($options['clean']) ? String::cleanInsert($str, $options) : $str;
225: }
226:
227: asort($data);
228:
229: $dataKeys = array_keys($data);
230: $hashKeys = array_map('crc32', $dataKeys);
231: $tempData = array_combine($dataKeys, $hashKeys);
232: krsort($tempData);
233:
234: foreach ($tempData as $key => $hashVal) {
235: $key = sprintf($format, preg_quote($key, '/'));
236: $str = preg_replace($key, $hashVal, $str);
237: }
238: $dataReplacements = array_combine($hashKeys, array_values($data));
239: foreach ($dataReplacements as $tmpHash => $tmpValue) {
240: $tmpValue = (is_array($tmpValue)) ? '' : $tmpValue;
241: $str = str_replace($tmpHash, $tmpValue, $str);
242: }
243:
244: if (!isset($options['format']) && isset($options['before'])) {
245: $str = str_replace($options['escape'] . $options['before'], $options['before'], $str);
246: }
247: return ($options['clean']) ? String::cleanInsert($str, $options) : $str;
248: }
249:
250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260:
261: public static function cleanInsert($str, $options) {
262: $clean = $options['clean'];
263: if (!$clean) {
264: return $str;
265: }
266: if ($clean === true) {
267: $clean = array('method' => 'text');
268: }
269: if (!is_array($clean)) {
270: $clean = array('method' => $options['clean']);
271: }
272: switch ($clean['method']) {
273: case 'html':
274: $clean = array_merge(array(
275: 'word' => '[\w,.]+',
276: 'andText' => true,
277: 'replacement' => '',
278: ), $clean);
279: $kleenex = sprintf(
280: '/[\s]*[a-z]+=(")(%s%s%s[\s]*)+\\1/i',
281: preg_quote($options['before'], '/'),
282: $clean['word'],
283: preg_quote($options['after'], '/')
284: );
285: $str = preg_replace($kleenex, $clean['replacement'], $str);
286: if ($clean['andText']) {
287: $options['clean'] = array('method' => 'text');
288: $str = String::cleanInsert($str, $options);
289: }
290: break;
291: case 'text':
292: $clean = array_merge(array(
293: 'word' => '[\w,.]+',
294: 'gap' => '[\s]*(?:(?:and|or)[\s]*)?',
295: 'replacement' => '',
296: ), $clean);
297:
298: $kleenex = sprintf(
299: '/(%s%s%s%s|%s%s%s%s)/',
300: preg_quote($options['before'], '/'),
301: $clean['word'],
302: preg_quote($options['after'], '/'),
303: $clean['gap'],
304: $clean['gap'],
305: preg_quote($options['before'], '/'),
306: $clean['word'],
307: preg_quote($options['after'], '/')
308: );
309: $str = preg_replace($kleenex, $clean['replacement'], $str);
310: break;
311: }
312: return $str;
313: }
314:
315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328:
329: public static function wrap($text, $options = array()) {
330: if (is_numeric($options)) {
331: $options = array('width' => $options);
332: }
333: $options += array('width' => 72, 'wordWrap' => true, 'indent' => null, 'indentAt' => 0);
334: if ($options['wordWrap']) {
335: $wrapped = self::wordWrap($text, $options['width'], "\n");
336: } else {
337: $wrapped = trim(chunk_split($text, $options['width'] - 1, "\n"));
338: }
339: if (!empty($options['indent'])) {
340: $chunks = explode("\n", $wrapped);
341: for ($i = $options['indentAt'], $len = count($chunks); $i < $len; $i++) {
342: $chunks[$i] = $options['indent'] . $chunks[$i];
343: }
344: $wrapped = implode("\n", $chunks);
345: }
346: return $wrapped;
347: }
348:
349: 350: 351: 352: 353: 354: 355: 356: 357:
358: public static function wordWrap($text, $width = 72, $break = "\n", $cut = false) {
359: if ($cut) {
360: $parts = array();
361: while (mb_strlen($text) > 0) {
362: $part = mb_substr($text, 0, $width);
363: $parts[] = trim($part);
364: $text = trim(mb_substr($text, mb_strlen($part)));
365: }
366: return implode($break, $parts);
367: }
368:
369: $parts = array();
370: while (mb_strlen($text) > 0) {
371: if ($width >= mb_strlen($text)) {
372: $parts[] = trim($text);
373: break;
374: }
375:
376: $part = mb_substr($text, 0, $width);
377: $nextChar = mb_substr($text, $width, 1);
378: if ($nextChar !== ' ') {
379: $breakAt = mb_strrpos($part, ' ');
380: if ($breakAt === false) {
381: $breakAt = mb_strpos($text, ' ', $width);
382: }
383: if ($breakAt === false) {
384: $parts[] = trim($text);
385: break;
386: }
387: $part = mb_substr($text, 0, $breakAt);
388: }
389:
390: $part = trim($part);
391: $parts[] = $part;
392: $text = trim(mb_substr($text, mb_strlen($part)));
393: }
394:
395: return implode($break, $parts);
396: }
397:
398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413:
414: public static function highlight($text, $phrase, $options = array()) {
415: if (empty($phrase)) {
416: return $text;
417: }
418:
419: $default = array(
420: 'format' => '<span class="highlight">\1</span>',
421: 'html' => false,
422: 'regex' => "|%s|iu"
423: );
424: $options = array_merge($default, $options);
425: extract($options);
426:
427: if (is_array($phrase)) {
428: $replace = array();
429: $with = array();
430:
431: foreach ($phrase as $key => $segment) {
432: $segment = '(' . preg_quote($segment, '|') . ')';
433: if ($html) {
434: $segment = "(?![^<]+>)$segment(?![^<]+>)";
435: }
436:
437: $with[] = (is_array($format)) ? $format[$key] : $format;
438: $replace[] = sprintf($options['regex'], $segment);
439: }
440:
441: return preg_replace($replace, $with, $text);
442: }
443:
444: $phrase = '(' . preg_quote($phrase, '|') . ')';
445: if ($html) {
446: $phrase = "(?![^<]+>)$phrase(?![^<]+>)";
447: }
448:
449: return preg_replace(sprintf($options['regex'], $phrase), $format, $text);
450: }
451:
452: 453: 454: 455: 456: 457: 458:
459: public static function stripLinks($text) {
460: return preg_replace('|<a\s+[^>]+>|im', '', preg_replace('|<\/a>|im', '', $text));
461: }
462:
463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476: 477: 478:
479: public static function tail($text, $length = 100, $options = array()) {
480: $default = array(
481: 'ellipsis' => '...', 'exact' => true
482: );
483: $options = array_merge($default, $options);
484: extract($options);
485:
486: if (!function_exists('mb_strlen')) {
487: class_exists('Multibyte');
488: }
489:
490: if (mb_strlen($text) <= $length) {
491: return $text;
492: }
493:
494: $truncate = mb_substr($text, mb_strlen($text) - $length + mb_strlen($ellipsis));
495: if (!$exact) {
496: $spacepos = mb_strpos($truncate, ' ');
497: $truncate = $spacepos === false ? '' : trim(mb_substr($truncate, $spacepos));
498: }
499:
500: return $ellipsis . $truncate;
501: }
502:
503: 504: 505: 506: 507: 508: 509: 510: 511: 512: 513: 514: 515: 516: 517: 518: 519: 520:
521: public static function truncate($text, $length = 100, $options = array()) {
522: $default = array(
523: 'ellipsis' => '...', 'exact' => true, 'html' => false
524: );
525: if (isset($options['ending'])) {
526: $default['ellipsis'] = $options['ending'];
527: } elseif (!empty($options['html']) && Configure::read('App.encoding') === 'UTF-8') {
528: $default['ellipsis'] = "\xe2\x80\xa6";
529: }
530: $options = array_merge($default, $options);
531: extract($options);
532:
533: if (!function_exists('mb_strlen')) {
534: class_exists('Multibyte');
535: }
536:
537: if ($html) {
538: if (mb_strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
539: return $text;
540: }
541: $totalLength = mb_strlen(strip_tags($ellipsis));
542: $openTags = array();
543: $truncate = '';
544:
545: preg_match_all('/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER);
546: foreach ($tags as $tag) {
547: if (!preg_match('/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/s', $tag[2])) {
548: if (preg_match('/<[\w]+[^>]*>/s', $tag[0])) {
549: array_unshift($openTags, $tag[2]);
550: } elseif (preg_match('/<\/([\w]+)[^>]*>/s', $tag[0], $closeTag)) {
551: $pos = array_search($closeTag[1], $openTags);
552: if ($pos !== false) {
553: array_splice($openTags, $pos, 1);
554: }
555: }
556: }
557: $truncate .= $tag[1];
558:
559: $contentLength = mb_strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $tag[3]));
560: if ($contentLength + $totalLength > $length) {
561: $left = $length - $totalLength;
562: $entitiesLength = 0;
563: if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', $tag[3], $entities, PREG_OFFSET_CAPTURE)) {
564: foreach ($entities[0] as $entity) {
565: if ($entity[1] + 1 - $entitiesLength <= $left) {
566: $left--;
567: $entitiesLength += mb_strlen($entity[0]);
568: } else {
569: break;
570: }
571: }
572: }
573:
574: $truncate .= mb_substr($tag[3], 0, $left + $entitiesLength);
575: break;
576: } else {
577: $truncate .= $tag[3];
578: $totalLength += $contentLength;
579: }
580: if ($totalLength >= $length) {
581: break;
582: }
583: }
584: } else {
585: if (mb_strlen($text) <= $length) {
586: return $text;
587: }
588: $truncate = mb_substr($text, 0, $length - mb_strlen($ellipsis));
589: }
590: if (!$exact) {
591: $spacepos = mb_strrpos($truncate, ' ');
592: if ($html) {
593: $truncateCheck = mb_substr($truncate, 0, $spacepos);
594: $lastOpenTag = mb_strrpos($truncateCheck, '<');
595: $lastCloseTag = mb_strrpos($truncateCheck, '>');
596: if ($lastOpenTag > $lastCloseTag) {
597: preg_match_all('/<[\w]+[^>]*>/s', $truncate, $lastTagMatches);
598: $lastTag = array_pop($lastTagMatches[0]);
599: $spacepos = mb_strrpos($truncate, $lastTag) + mb_strlen($lastTag);
600: }
601: $bits = mb_substr($truncate, $spacepos);
602: preg_match_all('/<\/([a-z]+)>/', $bits, $droppedTags, PREG_SET_ORDER);
603: if (!empty($droppedTags)) {
604: if (!empty($openTags)) {
605: foreach ($droppedTags as $closingTag) {
606: if (!in_array($closingTag[1], $openTags)) {
607: array_unshift($openTags, $closingTag[1]);
608: }
609: }
610: } else {
611: foreach ($droppedTags as $closingTag) {
612: $openTags[] = $closingTag[1];
613: }
614: }
615: }
616: }
617: $truncate = mb_substr($truncate, 0, $spacepos);
618: }
619: $truncate .= $ellipsis;
620:
621: if ($html) {
622: foreach ($openTags as $tag) {
623: $truncate .= '</' . $tag . '>';
624: }
625: }
626:
627: return $truncate;
628: }
629:
630: 631: 632: 633: 634: 635: 636: 637: 638: 639: 640:
641: public static function excerpt($text, $phrase, $radius = 100, $ellipsis = '...') {
642: if (empty($text) || empty($phrase)) {
643: return self::truncate($text, $radius * 2, array('ellipsis' => $ellipsis));
644: }
645:
646: $append = $prepend = $ellipsis;
647:
648: $phraseLen = mb_strlen($phrase);
649: $textLen = mb_strlen($text);
650:
651: $pos = mb_strpos(mb_strtolower($text), mb_strtolower($phrase));
652: if ($pos === false) {
653: return mb_substr($text, 0, $radius) . $ellipsis;
654: }
655:
656: $startPos = $pos - $radius;
657: if ($startPos <= 0) {
658: $startPos = 0;
659: $prepend = '';
660: }
661:
662: $endPos = $pos + $phraseLen + $radius;
663: if ($endPos >= $textLen) {
664: $endPos = $textLen;
665: $append = '';
666: }
667:
668: $excerpt = mb_substr($text, $startPos, $endPos - $startPos);
669: $excerpt = $prepend . $excerpt . $append;
670:
671: return $excerpt;
672: }
673:
674: 675: 676: 677: 678: 679: 680: 681: 682:
683: public static function toList($list, $and = 'and', $separator = ', ') {
684: if (count($list) > 1) {
685: return implode($separator, array_slice($list, null, -1)) . ' ' . $and . ' ' . array_pop($list);
686: }
687:
688: return array_pop($list);
689: }
690: }
691: