CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.4 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.4
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • CakeNumber
  • CakeTime
  • ClassRegistry
  • Debugger
  • File
  • Folder
  • Hash
  • Inflector
  • ObjectCollection
  • Sanitize
  • Security
  • Set
  • String
  • Validation
  • Xml
  1: <?php
  2: /**
  3:  * String handling methods.
  4:  *
  5:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6:  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7:  *
  8:  * Licensed under The MIT License
  9:  * For full copyright and license information, please see the LICENSE.txt
 10:  * Redistributions of files must retain the above copyright notice.
 11:  *
 12:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 13:  * @link          http://cakephp.org CakePHP(tm) Project
 14:  * @package       Cake.Utility
 15:  * @since         CakePHP(tm) v 1.2.0.5551
 16:  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 17:  */
 18: 
 19: /**
 20:  * String handling methods.
 21:  *
 22:  *
 23:  * @package       Cake.Utility
 24:  */
 25: class String {
 26: 
 27: /**
 28:  * Generate a random UUID
 29:  *
 30:  * @see http://www.ietf.org/rfc/rfc4122.txt
 31:  * @return RFC 4122 UUID
 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:  * Tokenizes a string using $separator, ignoring any instance of $separator that appears between
102:  * $leftBound and $rightBound
103:  *
104:  * @param string $data The data to tokenize
105:  * @param string $separator The token to split the data on.
106:  * @param string $leftBound The left boundary to ignore separators in.
107:  * @param string $rightBound The right boundary to ignore separators in.
108:  * @return mixed Array of tokens in $data or original input if empty.
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:  * Replaces variable placeholders inside a $str with any given $data. Each key in the $data array
178:  * corresponds to a variable placeholder name in $str.
179:  * Example: `String::insert(':name is :age years old.', array('name' => 'Bob', '65'));`
180:  * Returns: Bob is 65 years old.
181:  *
182:  * Available $options are:
183:  *
184:  * - before: The character or string in front of the name of the variable placeholder (Defaults to `:`)
185:  * - after: The character or string after the name of the variable placeholder (Defaults to null)
186:  * - escape: The character or string used to escape the before character / string (Defaults to `\`)
187:  * - format: A regex to use for matching variable placeholders. Default is: `/(?<!\\)\:%s/`
188:  *   (Overwrites before, after, breaks escape / clean)
189:  * - clean: A boolean or array with instructions for String::cleanInsert
190:  *
191:  * @param string $str A string containing variable placeholders
192:  * @param array $data A key => val array where each key stands for a placeholder variable name
193:  *     to be replaced with val
194:  * @param array $options An array of options, see description above
195:  * @return string
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:  * Cleans up a String::insert() formatted string with given $options depending on the 'clean' key in
252:  * $options. The default method used is text but html is also available. The goal of this function
253:  * is to replace all whitespace and unneeded markup around placeholders that did not get replaced
254:  * by String::insert().
255:  *
256:  * @param string $str
257:  * @param array $options
258:  * @return string
259:  * @see String::insert()
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:  * Wraps text to a specific width, can optionally wrap at word breaks.
317:  *
318:  * ### Options
319:  *
320:  * - `width` The width to wrap to. Defaults to 72.
321:  * - `wordWrap` Only wrap on words breaks (spaces) Defaults to true.
322:  * - `indent` String to indent with. Defaults to null.
323:  * - `indentAt` 0 based index to start indenting at. Defaults to 0.
324:  *
325:  * @param string $text The text to format.
326:  * @param array|integer $options Array of options to use, or an integer to wrap the text to.
327:  * @return string Formatted text.
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:  * Unicode aware version of wordwrap.
351:  *
352:  * @param string $text The text to format.
353:  * @param integer $width The width to wrap to. Defaults to 72.
354:  * @param string $break The line is broken using the optional break parameter. Defaults to '\n'.
355:  * @param boolean $cut If the cut is set to true, the string is always wrapped at the specified width.
356:  * @return string Formatted text.
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:  * Highlights a given phrase in a text. You can specify any expression in highlighter that
400:  * may include the \1 expression to include the $phrase found.
401:  *
402:  * ### Options:
403:  *
404:  * - `format` The piece of html with that the phrase will be highlighted
405:  * - `html` If true, will ignore any HTML tags, ensuring that only the correct text is highlighted
406:  * - `regex` a custom regex rule that is used to match words, default is '|$tag|iu'
407:  *
408:  * @param string $text Text to search the phrase in
409:  * @param string $phrase The phrase that will be searched
410:  * @param array $options An array of html attributes and options.
411:  * @return string The highlighted text
412:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::highlight
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:  * Strips given text of all links (<a href=....)
454:  *
455:  * @param string $text Text
456:  * @return string The text without links
457:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::stripLinks
458:  */
459:     public static function stripLinks($text) {
460:         return preg_replace('|<a\s+[^>]+>|im', '', preg_replace('|<\/a>|im', '', $text));
461:     }
462: 
463: /**
464:  * Truncates text starting from the end.
465:  *
466:  * Cuts a string to the length of $length and replaces the first characters
467:  * with the ellipsis if the text is longer than length.
468:  *
469:  * ### Options:
470:  *
471:  * - `ellipsis` Will be used as Beginning and prepended to the trimmed string
472:  * - `exact` If false, $text will not be cut mid-word
473:  *
474:  * @param string $text String to truncate.
475:  * @param integer $length Length of returned string, including ellipsis.
476:  * @param array $options An array of options.
477:  * @return string Trimmed string.
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:  * Truncates text.
505:  *
506:  * Cuts a string to the length of $length and replaces the last characters
507:  * with the ellipsis if the text is longer than length.
508:  *
509:  * ### Options:
510:  *
511:  * - `ellipsis` Will be used as Ending and appended to the trimmed string (`ending` is deprecated)
512:  * - `exact` If false, $text will not be cut mid-word
513:  * - `html` If true, HTML tags would be handled correctly
514:  *
515:  * @param string $text String to truncate.
516:  * @param integer $length Length of returned string, including ellipsis.
517:  * @param array $options An array of html attributes and options.
518:  * @return string Trimmed string.
519:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::truncate
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:  * Extracts an excerpt from the text surrounding the phrase with a number of characters on each side
632:  * determined by radius.
633:  *
634:  * @param string $text String to search the phrase in
635:  * @param string $phrase Phrase that will be searched for
636:  * @param integer $radius The amount of characters that will be returned on each side of the founded phrase
637:  * @param string $ellipsis Ending that will be appended
638:  * @return string Modified string
639:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::excerpt
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:  * Creates a comma separated list where the last two items are joined with 'and', forming natural English
676:  *
677:  * @param array $list The list to be joined
678:  * @param string $and The word used to join the last and second last items together with. Defaults to 'and'
679:  * @param string $separator The separator used to join all the other items together. Defaults to ', '
680:  * @return string The glued together string.
681:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::toList
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: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs