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