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.5 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.5
      • 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:  * @package       Cake.Utility
 23:  */
 24: class String {
 25: 
 26: /**
 27:  * Generate a random UUID
 28:  *
 29:  * @see http://www.ietf.org/rfc/rfc4122.txt
 30:  * @return RFC 4122 UUID
 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:  * Tokenizes a string using $separator, ignoring any instance of $separator that appears between
101:  * $leftBound and $rightBound.
102:  *
103:  * @param string $data The data to tokenize.
104:  * @param string $separator The token to split the data on.
105:  * @param string $leftBound The left boundary to ignore separators in.
106:  * @param string $rightBound The right boundary to ignore separators in.
107:  * @return mixed Array of tokens in $data or original input if empty.
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:  * Replaces variable placeholders inside a $str with any given $data. Each key in the $data array
177:  * corresponds to a variable placeholder name in $str.
178:  * Example: `String::insert(':name is :age years old.', array('name' => 'Bob', '65'));`
179:  * Returns: Bob is 65 years old.
180:  *
181:  * Available $options are:
182:  *
183:  * - before: The character or string in front of the name of the variable placeholder (Defaults to `:`)
184:  * - after: The character or string after the name of the variable placeholder (Defaults to null)
185:  * - escape: The character or string used to escape the before character / string (Defaults to `\`)
186:  * - format: A regex to use for matching variable placeholders. Default is: `/(?<!\\)\:%s/`
187:  *   (Overwrites before, after, breaks escape / clean)
188:  * - clean: A boolean or array with instructions for String::cleanInsert
189:  *
190:  * @param string $str A string containing variable placeholders
191:  * @param array $data A key => val array where each key stands for a placeholder variable name
192:  *     to be replaced with val
193:  * @param array $options An array of options, see description above
194:  * @return string
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:  * Cleans up a String::insert() formatted string with given $options depending on the 'clean' key in
251:  * $options. The default method used is text but html is also available. The goal of this function
252:  * is to replace all whitespace and unneeded markup around placeholders that did not get replaced
253:  * by String::insert().
254:  *
255:  * @param string $str String to clean.
256:  * @param array $options Options list.
257:  * @return string
258:  * @see String::insert()
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:  * Wraps text to a specific width, can optionally wrap at word breaks.
316:  *
317:  * ### Options
318:  *
319:  * - `width` The width to wrap to. Defaults to 72.
320:  * - `wordWrap` Only wrap on words breaks (spaces) Defaults to true.
321:  * - `indent` String to indent with. Defaults to null.
322:  * - `indentAt` 0 based index to start indenting at. Defaults to 0.
323:  *
324:  * @param string $text The text to format.
325:  * @param array|int $options Array of options to use, or an integer to wrap the text to.
326:  * @return string Formatted text.
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:  * Unicode aware version of wordwrap.
350:  *
351:  * @param string $text The text to format.
352:  * @param int $width The width to wrap to. Defaults to 72.
353:  * @param string $break The line is broken using the optional break parameter. Defaults to '\n'.
354:  * @param bool $cut If the cut is set to true, the string is always wrapped at the specified width.
355:  * @return string Formatted text.
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:  * Highlights a given phrase in a text. You can specify any expression in highlighter that
399:  * may include the \1 expression to include the $phrase found.
400:  *
401:  * ### Options:
402:  *
403:  * - `format` The piece of html with that the phrase will be highlighted
404:  * - `html` If true, will ignore any HTML tags, ensuring that only the correct text is highlighted
405:  * - `regex` a custom regex rule that is used to match words, default is '|$tag|iu'
406:  *
407:  * @param string $text Text to search the phrase in.
408:  * @param string|array $phrase The phrase or phrases that will be searched.
409:  * @param array $options An array of html attributes and options.
410:  * @return string The highlighted text
411:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::highlight
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:  * Strips given text of all links (<a href=....).
453:  *
454:  * @param string $text Text
455:  * @return string The text without links
456:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::stripLinks
457:  */
458:     public static function stripLinks($text) {
459:         return preg_replace('|<a\s+[^>]+>|im', '', preg_replace('|<\/a>|im', '', $text));
460:     }
461: 
462: /**
463:  * Truncates text starting from the end.
464:  *
465:  * Cuts a string to the length of $length and replaces the first characters
466:  * with the ellipsis if the text is longer than length.
467:  *
468:  * ### Options:
469:  *
470:  * - `ellipsis` Will be used as Beginning and prepended to the trimmed string
471:  * - `exact` If false, $text will not be cut mid-word
472:  *
473:  * @param string $text String to truncate.
474:  * @param int $length Length of returned string, including ellipsis.
475:  * @param array $options An array of options.
476:  * @return string Trimmed string.
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:  * Truncates text.
504:  *
505:  * Cuts a string to the length of $length and replaces the last characters
506:  * with the ellipsis if the text is longer than length.
507:  *
508:  * ### Options:
509:  *
510:  * - `ellipsis` Will be used as Ending and appended to the trimmed string (`ending` is deprecated)
511:  * - `exact` If false, $text will not be cut mid-word
512:  * - `html` If true, HTML tags would be handled correctly
513:  *
514:  * @param string $text String to truncate.
515:  * @param int $length Length of returned string, including ellipsis.
516:  * @param array $options An array of html attributes and options.
517:  * @return string Trimmed string.
518:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::truncate
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:  * Extracts an excerpt from the text surrounding the phrase with a number of characters on each side
631:  * determined by radius.
632:  *
633:  * @param string $text String to search the phrase in
634:  * @param string $phrase Phrase that will be searched for
635:  * @param int $radius The amount of characters that will be returned on each side of the founded phrase
636:  * @param string $ellipsis Ending that will be appended
637:  * @return string Modified string
638:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::excerpt
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:  * Creates a comma separated list where the last two items are joined with 'and', forming natural English
675:  *
676:  * @param array $list The list to be joined
677:  * @param string $and The word used to join the last and second last items together with. Defaults to 'and'
678:  * @param string $separator The separator used to join all the other items together. Defaults to ', '
679:  * @return string The glued together string.
680:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::toList
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: 
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