: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
$html = $format = $limit = null;
* @var string|array $format
foreach ($phrase as $key => $segment) {
$segment = '(' . preg_quote($segment, '|') . ')';
$segment = "(?![^<]+>)$segment(?![^<]+>)";
$with[] = is_array($format) ? $format[$key] : $format;
$replace[] = sprintf($options['regex'], $segment);
return preg_replace($replace, $with, $text, $limit);
$phrase = '(' . preg_quote($phrase, '|') . ')';
$phrase = "(?![^<]+>)$phrase(?![^<]+>)";
return preg_replace(sprintf($options['regex'], $phrase), $format, $text, $limit);
* Strips given text of all links (<a href=....).
* *Warning* This method is not an robust solution in preventing XSS
* @param string $text Text
* @return string The text without links
* @deprecated 3.2.12 This method will be removed in 4.0.0
public static function stripLinks($text)
deprecationWarning('This method will be removed in 4.0.0.');
$text = preg_replace('#</?a([/\s][^>]*)?(>|$)#i', '', $text, -1, $count);
* Truncates text starting from the end.
* Cuts a string to the length of $length and replaces the first characters
* with the ellipsis if the text is longer than length.
* - `ellipsis` Will be used as beginning and prepended to the trimmed string
* - `exact` If false, $text will not be cut mid-word
* @param string $text String to truncate.
* @param int $length Length of returned string, including ellipsis.
* @param array $options An array of options.
* @return string Trimmed string.
public static function tail($text, $length = 100, array $options = [])
'ellipsis' => '...', 'exact' => true,
$exact = $ellipsis = null;
if (mb_strlen($text) <= $length) {
$truncate = mb_substr($text, mb_strlen($text) - $length + mb_strlen($ellipsis));
$spacepos = mb_strpos($truncate, ' ');
$truncate = $spacepos === false ? '' : trim(mb_substr($truncate, $spacepos));
return $ellipsis . $truncate;
* Cuts a string to the length of $length and replaces the last characters
* with the ellipsis if the text is longer than length.
* - `ellipsis` Will be used as ending and appended to the trimmed string
* - `exact` If false, $text will not be cut mid-word
* - `html` If true, HTML tags would be handled correctly
* - `trimWidth` If true, $text will be truncated with the width
* @param string $text String to truncate.
* @param int $length Length of returned string, including ellipsis.
* @param array $options An array of HTML attributes and options.
* @return string Trimmed string.
* @link https://book.cakephp.org/3/en/core-libraries/text.html#truncating-text
public static function truncate($text, $length = 100, array $options = [])
'ellipsis' => '...', 'exact' => true, 'html' => false, 'trimWidth' => false,
if (!empty($options['html']) && strtolower(mb_internal_encoding()) === 'utf-8') {
$default['ellipsis'] = "\xe2\x80\xa6";
$suffix = $options['ellipsis'];
$ellipsisLength = self::_strlen(strip_tags($options['ellipsis']), $options);
preg_match_all('/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER);
foreach ($tags as $tag) {
if (!in_array($tag[2], static::$_defaultHtmlNoCount, true)) {
$contentLength = self::_strlen($tag[3], $options);
if (!preg_match('/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/i', $tag[2])) {
if (preg_match('/<[\w]+[^>]*>/', $tag[0])) {
array_unshift($openTags, $tag[2]);
} elseif (preg_match('/<\/([\w]+)[^>]*>/', $tag[0], $closeTag)) {
$pos = array_search($closeTag[1], $openTags, true);
array_splice($openTags, $pos, 1);
if ($totalLength + $contentLength + $ellipsisLength > $length) {
$truncateLength = $length - $totalLength;
$totalLength += $contentLength;
if ($totalLength > $length) {
if ($totalLength <= $length) {
$length = $truncateLength;
foreach ($openTags as $tag) {
$suffix .= '</' . $tag . '>';
if (self::_strlen($text, $options) <= $length) {
$ellipsisLength = self::_strlen($options['ellipsis'], $options);
$result = self::_substr($text, 0, $length - $ellipsisLength, $options);
if (!$options['exact']) {
if (self::_substr($text, $length - $ellipsisLength, 1, $options) !== ' ') {
$result = self::_removeLastWord($result);
// If result is empty, then we don't need to count ellipsis in the cut.
$result = self::_substr($text, 0, $length, $options);
return $prefix . $result . $suffix;
* Truncate text with specified width.
* @param string $text String to truncate.
* @param int $length Length of returned string, including ellipsis.
* @param array $options An array of HTML attributes and options.
* @return string Trimmed string.
* @see \Cake\Utility\Text::truncate()
public static function truncateByWidth($text, $length = 100, array $options = [])
return static::truncate($text, $length, ['trimWidth' => true] + $options);
* - `html` If true, HTML entities will be handled as decoded characters.
* - `trimWidth` If true, the width will return.
* @param string $text The string being checked for length
* @param array $options An array of options.
protected static function _strlen($text, array $options)
if (empty($options['trimWidth'])) {
if (empty($options['html'])) {
$pattern = '/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i';
$replace = preg_replace_callback(
function ($match) use ($strlen) {
$utf8 = html_entity_decode($match[0], ENT_HTML5 | ENT_QUOTES, 'UTF-8');
return str_repeat(' ', $strlen($utf8, 'UTF-8'));
return $strlen($replace);
* Return part of a string.
* - `html` If true, HTML entities will be handled as decoded characters.
* - `trimWidth` If true, will be truncated with specified width.
* @param string $text The input string.
* @param int $start The position to begin extracting.
* @param int $length The desired length.
* @param array $options An array of options.
protected static function _substr($text, $start, $length, array $options)
if (empty($options['trimWidth'])) {
$substr = 'mb_strimwidth';
$maxPosition = self::_strlen($text, ['trimWidth' => false] + $options);
if ($start >= $maxPosition) {
$length = self::_strlen($text, $options);
$text = self::_substr($text, $start, null, $options);
$length += self::_strlen($text, $options);
if (empty($options['html'])) {
return (string)$substr($text, $start, $length);
$pattern = '/(&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};)/i';
$parts = preg_split($pattern, $text, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
foreach ($parts as $part) {
if ($totalOffset < $start) {
$len = self::_strlen($part, ['trimWidth' => false] + $options);
if ($totalOffset + $len <= $start) {
$offset = $start - $totalOffset;
$len = self::_strlen($part, $options);
if ($offset !== 0 || $totalLength + $len > $length) {
strpos($part, '&') === 0 && preg_match($pattern, $part)
&& $part !== html_entity_decode($part, ENT_HTML5 | ENT_QUOTES, 'UTF-8')
// Entities cannot be passed substr.
$part = $substr($part, $offset, $length - $totalLength);
$len = self::_strlen($part, $options);
if ($totalLength >= $length) {
* Removes the last word from the input text.
* @param string $text The input text
protected static function _removeLastWord($text)
$spacepos = mb_strrpos($text, ' ');
if ($spacepos !== false) {
$lastWord = mb_strrpos($text, $spacepos);
// Some languages are written without word separation.
// We recognize a string as a word if it doesn't contain any full-width characters.
if (mb_strwidth($lastWord) === mb_strlen($lastWord)) {
$text = mb_substr($text, 0, $spacepos);
* Extracts an excerpt from the text surrounding the phrase with a number of characters on each side
* @param string $text String to search the phrase in
* @param string $phrase Phrase that will be searched for
* @param int $radius The amount of characters that will be returned on each side of the founded phrase
* @param string $ellipsis Ending that will be appended
* @return string Modified string
* @link https://book.cakephp.org/3/en/core-libraries/text.html#extracting-an-excerpt
public static function excerpt($text, $phrase, $radius = 100, $ellipsis = '...')
if (empty($text) || empty($phrase)) {
return static::truncate($text, $radius * 2, ['ellipsis' => $ellipsis]);
$append = $prepend = $ellipsis;
$phraseLen = mb_strlen($phrase);
$textLen = mb_strlen($text);
$pos = mb_stripos($text, $phrase);
return mb_substr($text, 0, $radius) . $ellipsis;
$startPos = $pos - $radius;
$endPos = $pos + $phraseLen + $radius;
if ($endPos >= $textLen) {
$excerpt = mb_substr($text, $startPos, $endPos - $startPos);
$excerpt = $prepend . $excerpt . $append;
* Creates a comma separated list where the last two items are joined with 'and', forming natural language.
* @param string[] $list The list to be joined.
* @param string|null $and The word used to join the last and second last items together with. Defaults to 'and'.
* @param string $separator The separator used to join all the other items together. Defaults to ', '.
* @return string The glued together string.
* @link https://book.cakephp.org/3/en/core-libraries/text.html#converting-an-array-to-sentence-form
public static function toList(array $list, $and = null, $separator = ', ')
$and = __d('cake', 'and');
return implode($separator, array_slice($list, null, -1)) . ' ' . $and . ' ' . array_pop($list);
return (string)array_pop($list);
* Check if the string contain multibyte characters
* @param string $string value to test
public static function isMultibyte($string)
$length = strlen($string);
for ($i = 0; $i < $length; $i++) {
$value = ord($string[$i]);
* Converts a multibyte character string
* to the decimal value of the character
* @param string $string String to convert.
public static function utf8($string)
$length = strlen($string);
for ($i = 0; $i < $length; $i++) {
$value = ord($string[$i]);
$find = ($value < 224) ? 2 : 3;
if (count($values) === $find) {
$map[] = (($values[0] % 16) * 4096) + (($values[1] % 64) * 64) + ($values[2] % 64);
$map[] = (($values[0] % 32) * 64) + ($values[1] % 64);