pictcode / lib / Cake / Utility / CakeText.php @ master
履歴 | 表示 | アノテート | ダウンロード (21.063 KB)
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 CakeText { |
25 |
|
26 |
/**
|
27 |
* Generate a random UUID
|
28 |
*
|
29 |
* @see http://www.ietf.org/rfc/rfc4122.txt
|
30 |
* @return string 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 = mb_strlen($data); |
119 |
$open = false; |
120 |
|
121 |
while ($offset <= $length) { |
122 |
$tmpOffset = -1; |
123 |
$offsets = array( |
124 |
mb_strpos($data, $separator, $offset), |
125 |
mb_strpos($data, $leftBound, $offset), |
126 |
mb_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 .= mb_substr($data, $offset, ($tmpOffset - $offset)); |
135 |
$char = mb_substr($data, $tmpOffset, 1); |
136 |
if (!$depth && $char === $separator) { |
137 |
$results[] = $buffer; |
138 |
$buffer = ''; |
139 |
} else {
|
140 |
$buffer .= $char; |
141 |
} |
142 |
if ($leftBound !== $rightBound) { |
143 |
if ($char === $leftBound) { |
144 |
$depth++;
|
145 |
} |
146 |
if ($char === $rightBound) { |
147 |
$depth--;
|
148 |
} |
149 |
} else {
|
150 |
if ($char === $leftBound) { |
151 |
if (!$open) { |
152 |
$depth++;
|
153 |
$open = true; |
154 |
} else {
|
155 |
$depth--;
|
156 |
} |
157 |
} |
158 |
} |
159 |
$offset = ++$tmpOffset; |
160 |
} else {
|
161 |
$results[] = $buffer . mb_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: `CakeText::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 CakeText::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']) ? CakeText::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']) ? CakeText::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']) ? CakeText::cleanInsert($str, $options) : $str; |
248 |
} |
249 |
|
250 |
/**
|
251 |
* Cleans up a CakeText::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 CakeText::insert().
|
255 |
*
|
256 |
* @param string $str CakeText to clean.
|
257 |
* @param array $options Options list.
|
258 |
* @return string
|
259 |
* @see CakeText::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 = CakeText::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` CakeText 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|int $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 = static::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 int $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 bool $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 |
$paragraphs = explode($break, $text); |
360 |
foreach ($paragraphs as &$paragraph) { |
361 |
$paragraph = static::_wordWrap($paragraph, $width, $break, $cut); |
362 |
} |
363 |
return implode($break, $paragraphs); |
364 |
} |
365 |
|
366 |
/**
|
367 |
* Helper method for wordWrap().
|
368 |
*
|
369 |
* @param string $text The text to format.
|
370 |
* @param int $width The width to wrap to. Defaults to 72.
|
371 |
* @param string $break The line is broken using the optional break parameter. Defaults to '\n'.
|
372 |
* @param bool $cut If the cut is set to true, the string is always wrapped at the specified width.
|
373 |
* @return string Formatted text.
|
374 |
*/
|
375 |
protected static function _wordWrap($text, $width = 72, $break = "\n", $cut = false) { |
376 |
if ($cut) { |
377 |
$parts = array(); |
378 |
while (mb_strlen($text) > 0) { |
379 |
$part = mb_substr($text, 0, $width); |
380 |
$parts[] = trim($part); |
381 |
$text = trim(mb_substr($text, mb_strlen($part))); |
382 |
} |
383 |
return implode($break, $parts); |
384 |
} |
385 |
|
386 |
$parts = array(); |
387 |
while (mb_strlen($text) > 0) { |
388 |
if ($width >= mb_strlen($text)) { |
389 |
$parts[] = trim($text); |
390 |
break;
|
391 |
} |
392 |
|
393 |
$part = mb_substr($text, 0, $width); |
394 |
$nextChar = mb_substr($text, $width, 1); |
395 |
if ($nextChar !== ' ') { |
396 |
$breakAt = mb_strrpos($part, ' '); |
397 |
if ($breakAt === false) { |
398 |
$breakAt = mb_strpos($text, ' ', $width); |
399 |
} |
400 |
if ($breakAt === false) { |
401 |
$parts[] = trim($text); |
402 |
break;
|
403 |
} |
404 |
$part = mb_substr($text, 0, $breakAt); |
405 |
} |
406 |
|
407 |
$part = trim($part); |
408 |
$parts[] = $part; |
409 |
$text = trim(mb_substr($text, mb_strlen($part))); |
410 |
} |
411 |
|
412 |
return implode($break, $parts); |
413 |
} |
414 |
|
415 |
/**
|
416 |
* Highlights a given phrase in a text. You can specify any expression in highlighter that
|
417 |
* may include the \1 expression to include the $phrase found.
|
418 |
*
|
419 |
* ### Options:
|
420 |
*
|
421 |
* - `format` The piece of html with that the phrase will be highlighted
|
422 |
* - `html` If true, will ignore any HTML tags, ensuring that only the correct text is highlighted
|
423 |
* - `regex` a custom regex rule that is used to match words, default is '|$tag|iu'
|
424 |
*
|
425 |
* @param string $text Text to search the phrase in.
|
426 |
* @param string|array $phrase The phrase or phrases that will be searched.
|
427 |
* @param array $options An array of html attributes and options.
|
428 |
* @return string The highlighted text
|
429 |
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::highlight
|
430 |
*/
|
431 |
public static function highlight($text, $phrase, $options = array()) { |
432 |
if (empty($phrase)) { |
433 |
return $text; |
434 |
} |
435 |
|
436 |
$defaults = array( |
437 |
'format' => '<span class="highlight">\1</span>', |
438 |
'html' => false, |
439 |
'regex' => "|%s|iu" |
440 |
); |
441 |
$options += $defaults; |
442 |
extract($options); |
443 |
|
444 |
if (is_array($phrase)) { |
445 |
$replace = array(); |
446 |
$with = array(); |
447 |
|
448 |
foreach ($phrase as $key => $segment) { |
449 |
$segment = '(' . preg_quote($segment, '|') . ')'; |
450 |
if ($html) { |
451 |
$segment = "(?![^<]+>)$segment(?![^<]+>)"; |
452 |
} |
453 |
|
454 |
$with[] = (is_array($format)) ? $format[$key] : $format; |
455 |
$replace[] = sprintf($options['regex'], $segment); |
456 |
} |
457 |
|
458 |
return preg_replace($replace, $with, $text); |
459 |
} |
460 |
|
461 |
$phrase = '(' . preg_quote($phrase, '|') . ')'; |
462 |
if ($html) { |
463 |
$phrase = "(?![^<]+>)$phrase(?![^<]+>)"; |
464 |
} |
465 |
|
466 |
return preg_replace(sprintf($options['regex'], $phrase), $format, $text); |
467 |
} |
468 |
|
469 |
/**
|
470 |
* Strips given text of all links (<a href=....).
|
471 |
*
|
472 |
* @param string $text Text
|
473 |
* @return string The text without links
|
474 |
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::stripLinks
|
475 |
*/
|
476 |
public static function stripLinks($text) { |
477 |
return preg_replace('|<a\s+[^>]+>|im', '', preg_replace('|<\/a>|im', '', $text)); |
478 |
} |
479 |
|
480 |
/**
|
481 |
* Truncates text starting from the end.
|
482 |
*
|
483 |
* Cuts a string to the length of $length and replaces the first characters
|
484 |
* with the ellipsis if the text is longer than length.
|
485 |
*
|
486 |
* ### Options:
|
487 |
*
|
488 |
* - `ellipsis` Will be used as Beginning and prepended to the trimmed string
|
489 |
* - `exact` If false, $text will not be cut mid-word
|
490 |
*
|
491 |
* @param string $text CakeText to truncate.
|
492 |
* @param int $length Length of returned string, including ellipsis.
|
493 |
* @param array $options An array of options.
|
494 |
* @return string Trimmed string.
|
495 |
*/
|
496 |
public static function tail($text, $length = 100, $options = array()) { |
497 |
$defaults = array( |
498 |
'ellipsis' => '...', 'exact' => true |
499 |
); |
500 |
$options += $defaults; |
501 |
extract($options); |
502 |
|
503 |
if (!function_exists('mb_strlen')) { |
504 |
class_exists('Multibyte'); |
505 |
} |
506 |
|
507 |
if (mb_strlen($text) <= $length) { |
508 |
return $text; |
509 |
} |
510 |
|
511 |
$truncate = mb_substr($text, mb_strlen($text) - $length + mb_strlen($ellipsis)); |
512 |
if (!$exact) { |
513 |
$spacepos = mb_strpos($truncate, ' '); |
514 |
$truncate = $spacepos === false ? '' : trim(mb_substr($truncate, $spacepos)); |
515 |
} |
516 |
|
517 |
return $ellipsis . $truncate; |
518 |
} |
519 |
|
520 |
/**
|
521 |
* Truncates text.
|
522 |
*
|
523 |
* Cuts a string to the length of $length and replaces the last characters
|
524 |
* with the ellipsis if the text is longer than length.
|
525 |
*
|
526 |
* ### Options:
|
527 |
*
|
528 |
* - `ellipsis` Will be used as Ending and appended to the trimmed string (`ending` is deprecated)
|
529 |
* - `exact` If false, $text will not be cut mid-word
|
530 |
* - `html` If true, HTML tags would be handled correctly
|
531 |
*
|
532 |
* @param string $text CakeText to truncate.
|
533 |
* @param int $length Length of returned string, including ellipsis.
|
534 |
* @param array $options An array of html attributes and options.
|
535 |
* @return string Trimmed string.
|
536 |
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::truncate
|
537 |
*/
|
538 |
public static function truncate($text, $length = 100, $options = array()) { |
539 |
$defaults = array( |
540 |
'ellipsis' => '...', 'exact' => true, 'html' => false |
541 |
); |
542 |
if (isset($options['ending'])) { |
543 |
$defaults['ellipsis'] = $options['ending']; |
544 |
} elseif (!empty($options['html']) && Configure::read('App.encoding') === 'UTF-8') { |
545 |
$defaults['ellipsis'] = "\xe2\x80\xa6"; |
546 |
} |
547 |
$options += $defaults; |
548 |
extract($options); |
549 |
|
550 |
if (!function_exists('mb_strlen')) { |
551 |
class_exists('Multibyte'); |
552 |
} |
553 |
|
554 |
if ($html) { |
555 |
if (mb_strlen(preg_replace('/<.*?>/', '', $text)) <= $length) { |
556 |
return $text; |
557 |
} |
558 |
$totalLength = mb_strlen(strip_tags($ellipsis)); |
559 |
$openTags = array(); |
560 |
$truncate = ''; |
561 |
|
562 |
preg_match_all('/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER); |
563 |
foreach ($tags as $tag) { |
564 |
if (!preg_match('/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/s', $tag[2])) { |
565 |
if (preg_match('/<[\w]+[^>]*>/s', $tag[0])) { |
566 |
array_unshift($openTags, $tag[2]); |
567 |
} elseif (preg_match('/<\/([\w]+)[^>]*>/s', $tag[0], $closeTag)) { |
568 |
$pos = array_search($closeTag[1], $openTags); |
569 |
if ($pos !== false) { |
570 |
array_splice($openTags, $pos, 1); |
571 |
} |
572 |
} |
573 |
} |
574 |
$truncate .= $tag[1]; |
575 |
|
576 |
$contentLength = mb_strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $tag[3])); |
577 |
if ($contentLength + $totalLength > $length) { |
578 |
$left = $length - $totalLength; |
579 |
$entitiesLength = 0; |
580 |
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)) { |
581 |
foreach ($entities[0] as $entity) { |
582 |
if ($entity[1] + 1 - $entitiesLength <= $left) { |
583 |
$left--;
|
584 |
$entitiesLength += mb_strlen($entity[0]); |
585 |
} else {
|
586 |
break;
|
587 |
} |
588 |
} |
589 |
} |
590 |
|
591 |
$truncate .= mb_substr($tag[3], 0, $left + $entitiesLength); |
592 |
break;
|
593 |
} else {
|
594 |
$truncate .= $tag[3]; |
595 |
$totalLength += $contentLength; |
596 |
} |
597 |
if ($totalLength >= $length) { |
598 |
break;
|
599 |
} |
600 |
} |
601 |
} else {
|
602 |
if (mb_strlen($text) <= $length) { |
603 |
return $text; |
604 |
} |
605 |
$truncate = mb_substr($text, 0, $length - mb_strlen($ellipsis)); |
606 |
} |
607 |
if (!$exact) { |
608 |
$spacepos = mb_strrpos($truncate, ' '); |
609 |
if ($html) { |
610 |
$truncateCheck = mb_substr($truncate, 0, $spacepos); |
611 |
$lastOpenTag = mb_strrpos($truncateCheck, '<'); |
612 |
$lastCloseTag = mb_strrpos($truncateCheck, '>'); |
613 |
if ($lastOpenTag > $lastCloseTag) { |
614 |
preg_match_all('/<[\w]+[^>]*>/s', $truncate, $lastTagMatches); |
615 |
$lastTag = array_pop($lastTagMatches[0]); |
616 |
$spacepos = mb_strrpos($truncate, $lastTag) + mb_strlen($lastTag); |
617 |
} |
618 |
$bits = mb_substr($truncate, $spacepos); |
619 |
preg_match_all('/<\/([a-z]+)>/', $bits, $droppedTags, PREG_SET_ORDER); |
620 |
if (!empty($droppedTags)) { |
621 |
if (!empty($openTags)) { |
622 |
foreach ($droppedTags as $closingTag) { |
623 |
if (!in_array($closingTag[1], $openTags)) { |
624 |
array_unshift($openTags, $closingTag[1]); |
625 |
} |
626 |
} |
627 |
} else {
|
628 |
foreach ($droppedTags as $closingTag) { |
629 |
$openTags[] = $closingTag[1]; |
630 |
} |
631 |
} |
632 |
} |
633 |
} |
634 |
$truncate = mb_substr($truncate, 0, $spacepos); |
635 |
} |
636 |
$truncate .= $ellipsis; |
637 |
|
638 |
if ($html) { |
639 |
foreach ($openTags as $tag) { |
640 |
$truncate .= '</' . $tag . '>'; |
641 |
} |
642 |
} |
643 |
|
644 |
return $truncate; |
645 |
} |
646 |
|
647 |
/**
|
648 |
* Extracts an excerpt from the text surrounding the phrase with a number of characters on each side
|
649 |
* determined by radius.
|
650 |
*
|
651 |
* @param string $text CakeText to search the phrase in
|
652 |
* @param string $phrase Phrase that will be searched for
|
653 |
* @param int $radius The amount of characters that will be returned on each side of the founded phrase
|
654 |
* @param string $ellipsis Ending that will be appended
|
655 |
* @return string Modified string
|
656 |
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::excerpt
|
657 |
*/
|
658 |
public static function excerpt($text, $phrase, $radius = 100, $ellipsis = '...') { |
659 |
if (empty($text) || empty($phrase)) { |
660 |
return static::truncate($text, $radius * 2, array('ellipsis' => $ellipsis)); |
661 |
} |
662 |
|
663 |
$append = $prepend = $ellipsis; |
664 |
|
665 |
$phraseLen = mb_strlen($phrase); |
666 |
$textLen = mb_strlen($text); |
667 |
|
668 |
$pos = mb_strpos(mb_strtolower($text), mb_strtolower($phrase)); |
669 |
if ($pos === false) { |
670 |
return mb_substr($text, 0, $radius) . $ellipsis; |
671 |
} |
672 |
|
673 |
$startPos = $pos - $radius; |
674 |
if ($startPos <= 0) { |
675 |
$startPos = 0; |
676 |
$prepend = ''; |
677 |
} |
678 |
|
679 |
$endPos = $pos + $phraseLen + $radius; |
680 |
if ($endPos >= $textLen) { |
681 |
$endPos = $textLen; |
682 |
$append = ''; |
683 |
} |
684 |
|
685 |
$excerpt = mb_substr($text, $startPos, $endPos - $startPos); |
686 |
$excerpt = $prepend . $excerpt . $append; |
687 |
|
688 |
return $excerpt; |
689 |
} |
690 |
|
691 |
/**
|
692 |
* Creates a comma separated list where the last two items are joined with 'and', forming natural language.
|
693 |
*
|
694 |
* @param array $list The list to be joined.
|
695 |
* @param string $and The word used to join the last and second last items together with. Defaults to 'and'.
|
696 |
* @param string $separator The separator used to join all the other items together. Defaults to ', '.
|
697 |
* @return string The glued together string.
|
698 |
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::toList
|
699 |
*/
|
700 |
public static function toList($list, $and = null, $separator = ', ') { |
701 |
if ($and === null) { |
702 |
$and = __d('cake', 'and'); |
703 |
} |
704 |
if (count($list) > 1) { |
705 |
return implode($separator, array_slice($list, null, -1)) . ' ' . $and . ' ' . array_pop($list); |
706 |
} |
707 |
|
708 |
return array_pop($list); |
709 |
} |
710 |
} |