Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/clone/wp-conte.../plugins/flow-flo.../libs/cakephp/utility
File: Text.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
[2] Fix | Delete
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
[3] Fix | Delete
*
[4] Fix | Delete
* Licensed under The MIT License
[5] Fix | Delete
* For full copyright and license information, please see the LICENSE.txt
[6] Fix | Delete
* Redistributions of files must retain the above copyright notice.
[7] Fix | Delete
*
[8] Fix | Delete
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
[9] Fix | Delete
* @link https://cakephp.org CakePHP(tm) Project
[10] Fix | Delete
* @since 1.2.0
[11] Fix | Delete
* @license https://opensource.org/licenses/mit-license.php MIT License
[12] Fix | Delete
*/
[13] Fix | Delete
namespace Cake\Utility;
[14] Fix | Delete
[15] Fix | Delete
use Cake\Core\Exception\Exception;
[16] Fix | Delete
use InvalidArgumentException;
[17] Fix | Delete
[18] Fix | Delete
/**
[19] Fix | Delete
* Text handling methods.
[20] Fix | Delete
*/
[21] Fix | Delete
class Text
[22] Fix | Delete
{
[23] Fix | Delete
/**
[24] Fix | Delete
* Default transliterator.
[25] Fix | Delete
*
[26] Fix | Delete
* @var \Transliterator|null Transliterator instance.
[27] Fix | Delete
*/
[28] Fix | Delete
protected static $_defaultTransliterator;
[29] Fix | Delete
[30] Fix | Delete
/**
[31] Fix | Delete
* Default transliterator id string.
[32] Fix | Delete
*
[33] Fix | Delete
* @var string $_defaultTransliteratorId Transliterator identifier string.
[34] Fix | Delete
*/
[35] Fix | Delete
protected static $_defaultTransliteratorId = 'Any-Latin; Latin-ASCII; [\u0080-\u7fff] remove';
[36] Fix | Delete
[37] Fix | Delete
/**
[38] Fix | Delete
* Default html tags who must not be count for truncate text.
[39] Fix | Delete
*
[40] Fix | Delete
* @var array
[41] Fix | Delete
*/
[42] Fix | Delete
protected static $_defaultHtmlNoCount = [
[43] Fix | Delete
'style',
[44] Fix | Delete
'script',
[45] Fix | Delete
];
[46] Fix | Delete
[47] Fix | Delete
/**
[48] Fix | Delete
* Generate a random UUID version 4
[49] Fix | Delete
*
[50] Fix | Delete
* Warning: This method should not be used as a random seed for any cryptographic operations.
[51] Fix | Delete
* Instead you should use the openssl or mcrypt extensions.
[52] Fix | Delete
*
[53] Fix | Delete
* It should also not be used to create identifiers that have security implications, such as
[54] Fix | Delete
* 'unguessable' URL identifiers. Instead you should use `Security::randomBytes()` for that.
[55] Fix | Delete
*
[56] Fix | Delete
* @see https://www.ietf.org/rfc/rfc4122.txt
[57] Fix | Delete
* @return string RFC 4122 UUID
[58] Fix | Delete
* @copyright Matt Farina MIT License https://github.com/lootils/uuid/blob/master/LICENSE
[59] Fix | Delete
*/
[60] Fix | Delete
public static function uuid()
[61] Fix | Delete
{
[62] Fix | Delete
return sprintf(
[63] Fix | Delete
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
[64] Fix | Delete
// 32 bits for "time_low"
[65] Fix | Delete
random_int(0, 65535),
[66] Fix | Delete
random_int(0, 65535),
[67] Fix | Delete
// 16 bits for "time_mid"
[68] Fix | Delete
random_int(0, 65535),
[69] Fix | Delete
// 12 bits before the 0100 of (version) 4 for "time_hi_and_version"
[70] Fix | Delete
random_int(0, 4095) | 0x4000,
[71] Fix | Delete
// 16 bits, 8 bits for "clk_seq_hi_res",
[72] Fix | Delete
// 8 bits for "clk_seq_low",
[73] Fix | Delete
// two most significant bits holds zero and one for variant DCE1.1
[74] Fix | Delete
random_int(0, 0x3fff) | 0x8000,
[75] Fix | Delete
// 48 bits for "node"
[76] Fix | Delete
random_int(0, 65535),
[77] Fix | Delete
random_int(0, 65535),
[78] Fix | Delete
random_int(0, 65535)
[79] Fix | Delete
);
[80] Fix | Delete
}
[81] Fix | Delete
[82] Fix | Delete
/**
[83] Fix | Delete
* Tokenizes a string using $separator, ignoring any instance of $separator that appears between
[84] Fix | Delete
* $leftBound and $rightBound.
[85] Fix | Delete
*
[86] Fix | Delete
* @param string $data The data to tokenize.
[87] Fix | Delete
* @param string $separator The token to split the data on.
[88] Fix | Delete
* @param string $leftBound The left boundary to ignore separators in.
[89] Fix | Delete
* @param string $rightBound The right boundary to ignore separators in.
[90] Fix | Delete
* @return string|string[] Array of tokens in $data or original input if empty.
[91] Fix | Delete
*/
[92] Fix | Delete
public static function tokenize($data, $separator = ',', $leftBound = '(', $rightBound = ')')
[93] Fix | Delete
{
[94] Fix | Delete
if (empty($data)) {
[95] Fix | Delete
return [];
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
$depth = 0;
[99] Fix | Delete
$offset = 0;
[100] Fix | Delete
$buffer = '';
[101] Fix | Delete
$results = [];
[102] Fix | Delete
$length = mb_strlen($data);
[103] Fix | Delete
$open = false;
[104] Fix | Delete
[105] Fix | Delete
while ($offset <= $length) {
[106] Fix | Delete
$tmpOffset = -1;
[107] Fix | Delete
$offsets = [
[108] Fix | Delete
mb_strpos($data, $separator, $offset),
[109] Fix | Delete
mb_strpos($data, $leftBound, $offset),
[110] Fix | Delete
mb_strpos($data, $rightBound, $offset),
[111] Fix | Delete
];
[112] Fix | Delete
for ($i = 0; $i < 3; $i++) {
[113] Fix | Delete
if ($offsets[$i] !== false && ($offsets[$i] < $tmpOffset || $tmpOffset == -1)) {
[114] Fix | Delete
$tmpOffset = $offsets[$i];
[115] Fix | Delete
}
[116] Fix | Delete
}
[117] Fix | Delete
if ($tmpOffset !== -1) {
[118] Fix | Delete
$buffer .= mb_substr($data, $offset, $tmpOffset - $offset);
[119] Fix | Delete
$char = mb_substr($data, $tmpOffset, 1);
[120] Fix | Delete
if (!$depth && $char === $separator) {
[121] Fix | Delete
$results[] = $buffer;
[122] Fix | Delete
$buffer = '';
[123] Fix | Delete
} else {
[124] Fix | Delete
$buffer .= $char;
[125] Fix | Delete
}
[126] Fix | Delete
if ($leftBound !== $rightBound) {
[127] Fix | Delete
if ($char === $leftBound) {
[128] Fix | Delete
$depth++;
[129] Fix | Delete
}
[130] Fix | Delete
if ($char === $rightBound) {
[131] Fix | Delete
$depth--;
[132] Fix | Delete
}
[133] Fix | Delete
} else {
[134] Fix | Delete
if ($char === $leftBound) {
[135] Fix | Delete
if (!$open) {
[136] Fix | Delete
$depth++;
[137] Fix | Delete
$open = true;
[138] Fix | Delete
} else {
[139] Fix | Delete
$depth--;
[140] Fix | Delete
$open = false;
[141] Fix | Delete
}
[142] Fix | Delete
}
[143] Fix | Delete
}
[144] Fix | Delete
$tmpOffset += 1;
[145] Fix | Delete
$offset = $tmpOffset;
[146] Fix | Delete
} else {
[147] Fix | Delete
$results[] = $buffer . mb_substr($data, $offset);
[148] Fix | Delete
$offset = $length + 1;
[149] Fix | Delete
}
[150] Fix | Delete
}
[151] Fix | Delete
if (empty($results) && !empty($buffer)) {
[152] Fix | Delete
$results[] = $buffer;
[153] Fix | Delete
}
[154] Fix | Delete
[155] Fix | Delete
if (!empty($results)) {
[156] Fix | Delete
return array_map('trim', $results);
[157] Fix | Delete
}
[158] Fix | Delete
[159] Fix | Delete
return [];
[160] Fix | Delete
}
[161] Fix | Delete
[162] Fix | Delete
/**
[163] Fix | Delete
* Replaces variable placeholders inside a $str with any given $data. Each key in the $data array
[164] Fix | Delete
* corresponds to a variable placeholder name in $str.
[165] Fix | Delete
* Example:
[166] Fix | Delete
* ```
[167] Fix | Delete
* Text::insert(':name is :age years old.', ['name' => 'Bob', 'age' => '65']);
[168] Fix | Delete
* ```
[169] Fix | Delete
* Returns: Bob is 65 years old.
[170] Fix | Delete
*
[171] Fix | Delete
* Available $options are:
[172] Fix | Delete
*
[173] Fix | Delete
* - before: The character or string in front of the name of the variable placeholder (Defaults to `:`)
[174] Fix | Delete
* - after: The character or string after the name of the variable placeholder (Defaults to null)
[175] Fix | Delete
* - escape: The character or string used to escape the before character / string (Defaults to `\`)
[176] Fix | Delete
* - format: A regex to use for matching variable placeholders. Default is: `/(?<!\\)\:%s/`
[177] Fix | Delete
* (Overwrites before, after, breaks escape / clean)
[178] Fix | Delete
* - clean: A boolean or array with instructions for Text::cleanInsert
[179] Fix | Delete
*
[180] Fix | Delete
* @param string $str A string containing variable placeholders
[181] Fix | Delete
* @param array $data A key => val array where each key stands for a placeholder variable name
[182] Fix | Delete
* to be replaced with val
[183] Fix | Delete
* @param array $options An array of options, see description above
[184] Fix | Delete
* @return string
[185] Fix | Delete
*/
[186] Fix | Delete
public static function insert($str, $data, array $options = [])
[187] Fix | Delete
{
[188] Fix | Delete
$defaults = [
[189] Fix | Delete
'before' => ':', 'after' => null, 'escape' => '\\', 'format' => null, 'clean' => false,
[190] Fix | Delete
];
[191] Fix | Delete
$options += $defaults;
[192] Fix | Delete
$format = $options['format'];
[193] Fix | Delete
$data = (array)$data;
[194] Fix | Delete
if (empty($data)) {
[195] Fix | Delete
return $options['clean'] ? static::cleanInsert($str, $options) : $str;
[196] Fix | Delete
}
[197] Fix | Delete
[198] Fix | Delete
if (!isset($format)) {
[199] Fix | Delete
$format = sprintf(
[200] Fix | Delete
'/(?<!%s)%s%%s%s/',
[201] Fix | Delete
preg_quote($options['escape'], '/'),
[202] Fix | Delete
str_replace('%', '%%', preg_quote($options['before'], '/')),
[203] Fix | Delete
str_replace('%', '%%', preg_quote($options['after'], '/'))
[204] Fix | Delete
);
[205] Fix | Delete
}
[206] Fix | Delete
[207] Fix | Delete
if (strpos($str, '?') !== false && is_numeric(key($data))) {
[208] Fix | Delete
$offset = 0;
[209] Fix | Delete
while (($pos = strpos($str, '?', $offset)) !== false) {
[210] Fix | Delete
$val = array_shift($data);
[211] Fix | Delete
$offset = $pos + strlen($val);
[212] Fix | Delete
$str = substr_replace($str, $val, $pos, 1);
[213] Fix | Delete
}
[214] Fix | Delete
[215] Fix | Delete
return $options['clean'] ? static::cleanInsert($str, $options) : $str;
[216] Fix | Delete
}
[217] Fix | Delete
[218] Fix | Delete
$dataKeys = array_keys($data);
[219] Fix | Delete
$hashKeys = array_map('crc32', $dataKeys);
[220] Fix | Delete
$tempData = array_combine($dataKeys, $hashKeys);
[221] Fix | Delete
krsort($tempData);
[222] Fix | Delete
[223] Fix | Delete
foreach ($tempData as $key => $hashVal) {
[224] Fix | Delete
$key = sprintf($format, preg_quote($key, '/'));
[225] Fix | Delete
$str = preg_replace($key, $hashVal, $str);
[226] Fix | Delete
}
[227] Fix | Delete
$dataReplacements = array_combine($hashKeys, array_values($data));
[228] Fix | Delete
foreach ($dataReplacements as $tmpHash => $tmpValue) {
[229] Fix | Delete
$tmpValue = is_array($tmpValue) ? '' : $tmpValue;
[230] Fix | Delete
$str = str_replace($tmpHash, $tmpValue, $str);
[231] Fix | Delete
}
[232] Fix | Delete
[233] Fix | Delete
if (!isset($options['format']) && isset($options['before'])) {
[234] Fix | Delete
$str = str_replace($options['escape'] . $options['before'], $options['before'], $str);
[235] Fix | Delete
}
[236] Fix | Delete
[237] Fix | Delete
return $options['clean'] ? static::cleanInsert($str, $options) : $str;
[238] Fix | Delete
}
[239] Fix | Delete
[240] Fix | Delete
/**
[241] Fix | Delete
* Cleans up a Text::insert() formatted string with given $options depending on the 'clean' key in
[242] Fix | Delete
* $options. The default method used is text but html is also available. The goal of this function
[243] Fix | Delete
* is to replace all whitespace and unneeded markup around placeholders that did not get replaced
[244] Fix | Delete
* by Text::insert().
[245] Fix | Delete
*
[246] Fix | Delete
* @param string $str String to clean.
[247] Fix | Delete
* @param array $options Options list.
[248] Fix | Delete
* @return string
[249] Fix | Delete
* @see \Cake\Utility\Text::insert()
[250] Fix | Delete
*/
[251] Fix | Delete
public static function cleanInsert($str, array $options)
[252] Fix | Delete
{
[253] Fix | Delete
$clean = $options['clean'];
[254] Fix | Delete
if (!$clean) {
[255] Fix | Delete
return $str;
[256] Fix | Delete
}
[257] Fix | Delete
if ($clean === true) {
[258] Fix | Delete
$clean = ['method' => 'text'];
[259] Fix | Delete
}
[260] Fix | Delete
if (!is_array($clean)) {
[261] Fix | Delete
$clean = ['method' => $options['clean']];
[262] Fix | Delete
}
[263] Fix | Delete
switch ($clean['method']) {
[264] Fix | Delete
case 'html':
[265] Fix | Delete
$clean += [
[266] Fix | Delete
'word' => '[\w,.]+',
[267] Fix | Delete
'andText' => true,
[268] Fix | Delete
'replacement' => '',
[269] Fix | Delete
];
[270] Fix | Delete
$kleenex = sprintf(
[271] Fix | Delete
'/[\s]*[a-z]+=(")(%s%s%s[\s]*)+\\1/i',
[272] Fix | Delete
preg_quote($options['before'], '/'),
[273] Fix | Delete
$clean['word'],
[274] Fix | Delete
preg_quote($options['after'], '/')
[275] Fix | Delete
);
[276] Fix | Delete
$str = preg_replace($kleenex, $clean['replacement'], $str);
[277] Fix | Delete
if ($clean['andText']) {
[278] Fix | Delete
$options['clean'] = ['method' => 'text'];
[279] Fix | Delete
$str = static::cleanInsert($str, $options);
[280] Fix | Delete
}
[281] Fix | Delete
break;
[282] Fix | Delete
case 'text':
[283] Fix | Delete
$clean += [
[284] Fix | Delete
'word' => '[\w,.]+',
[285] Fix | Delete
'gap' => '[\s]*(?:(?:and|or)[\s]*)?',
[286] Fix | Delete
'replacement' => '',
[287] Fix | Delete
];
[288] Fix | Delete
[289] Fix | Delete
$kleenex = sprintf(
[290] Fix | Delete
'/(%s%s%s%s|%s%s%s%s)/',
[291] Fix | Delete
preg_quote($options['before'], '/'),
[292] Fix | Delete
$clean['word'],
[293] Fix | Delete
preg_quote($options['after'], '/'),
[294] Fix | Delete
$clean['gap'],
[295] Fix | Delete
$clean['gap'],
[296] Fix | Delete
preg_quote($options['before'], '/'),
[297] Fix | Delete
$clean['word'],
[298] Fix | Delete
preg_quote($options['after'], '/')
[299] Fix | Delete
);
[300] Fix | Delete
$str = preg_replace($kleenex, $clean['replacement'], $str);
[301] Fix | Delete
break;
[302] Fix | Delete
}
[303] Fix | Delete
[304] Fix | Delete
return $str;
[305] Fix | Delete
}
[306] Fix | Delete
[307] Fix | Delete
/**
[308] Fix | Delete
* Wraps text to a specific width, can optionally wrap at word breaks.
[309] Fix | Delete
*
[310] Fix | Delete
* ### Options
[311] Fix | Delete
*
[312] Fix | Delete
* - `width` The width to wrap to. Defaults to 72.
[313] Fix | Delete
* - `wordWrap` Only wrap on words breaks (spaces) Defaults to true.
[314] Fix | Delete
* - `indent` String to indent with. Defaults to null.
[315] Fix | Delete
* - `indentAt` 0 based index to start indenting at. Defaults to 0.
[316] Fix | Delete
*
[317] Fix | Delete
* @param string $text The text to format.
[318] Fix | Delete
* @param array|int $options Array of options to use, or an integer to wrap the text to.
[319] Fix | Delete
* @return string Formatted text.
[320] Fix | Delete
*/
[321] Fix | Delete
public static function wrap($text, $options = [])
[322] Fix | Delete
{
[323] Fix | Delete
if (is_numeric($options)) {
[324] Fix | Delete
$options = ['width' => $options];
[325] Fix | Delete
}
[326] Fix | Delete
$options += ['width' => 72, 'wordWrap' => true, 'indent' => null, 'indentAt' => 0];
[327] Fix | Delete
if ($options['wordWrap']) {
[328] Fix | Delete
$wrapped = self::wordWrap($text, $options['width'], "\n");
[329] Fix | Delete
} else {
[330] Fix | Delete
$wrapped = trim(chunk_split($text, $options['width'] - 1, "\n"));
[331] Fix | Delete
}
[332] Fix | Delete
if (!empty($options['indent'])) {
[333] Fix | Delete
$chunks = explode("\n", $wrapped);
[334] Fix | Delete
for ($i = $options['indentAt'], $len = count($chunks); $i < $len; $i++) {
[335] Fix | Delete
$chunks[$i] = $options['indent'] . $chunks[$i];
[336] Fix | Delete
}
[337] Fix | Delete
$wrapped = implode("\n", $chunks);
[338] Fix | Delete
}
[339] Fix | Delete
[340] Fix | Delete
return $wrapped;
[341] Fix | Delete
}
[342] Fix | Delete
[343] Fix | Delete
/**
[344] Fix | Delete
* Wraps a complete block of text to a specific width, can optionally wrap
[345] Fix | Delete
* at word breaks.
[346] Fix | Delete
*
[347] Fix | Delete
* ### Options
[348] Fix | Delete
*
[349] Fix | Delete
* - `width` The width to wrap to. Defaults to 72.
[350] Fix | Delete
* - `wordWrap` Only wrap on words breaks (spaces) Defaults to true.
[351] Fix | Delete
* - `indent` String to indent with. Defaults to null.
[352] Fix | Delete
* - `indentAt` 0 based index to start indenting at. Defaults to 0.
[353] Fix | Delete
*
[354] Fix | Delete
* @param string $text The text to format.
[355] Fix | Delete
* @param array|int $options Array of options to use, or an integer to wrap the text to.
[356] Fix | Delete
* @return string Formatted text.
[357] Fix | Delete
*/
[358] Fix | Delete
public static function wrapBlock($text, $options = [])
[359] Fix | Delete
{
[360] Fix | Delete
if (is_numeric($options)) {
[361] Fix | Delete
$options = ['width' => $options];
[362] Fix | Delete
}
[363] Fix | Delete
$options += ['width' => 72, 'wordWrap' => true, 'indent' => null, 'indentAt' => 0];
[364] Fix | Delete
[365] Fix | Delete
if (!empty($options['indentAt']) && $options['indentAt'] === 0) {
[366] Fix | Delete
$indentLength = !empty($options['indent']) ? strlen($options['indent']) : 0;
[367] Fix | Delete
$options['width'] -= $indentLength;
[368] Fix | Delete
[369] Fix | Delete
return self::wrap($text, $options);
[370] Fix | Delete
}
[371] Fix | Delete
[372] Fix | Delete
$wrapped = self::wrap($text, $options);
[373] Fix | Delete
[374] Fix | Delete
if (!empty($options['indent'])) {
[375] Fix | Delete
$indentationLength = mb_strlen($options['indent']);
[376] Fix | Delete
$chunks = explode("\n", $wrapped);
[377] Fix | Delete
$count = count($chunks);
[378] Fix | Delete
if ($count < 2) {
[379] Fix | Delete
return $wrapped;
[380] Fix | Delete
}
[381] Fix | Delete
$toRewrap = '';
[382] Fix | Delete
for ($i = $options['indentAt']; $i < $count; $i++) {
[383] Fix | Delete
$toRewrap .= mb_substr($chunks[$i], $indentationLength) . ' ';
[384] Fix | Delete
unset($chunks[$i]);
[385] Fix | Delete
}
[386] Fix | Delete
$options['width'] -= $indentationLength;
[387] Fix | Delete
$options['indentAt'] = 0;
[388] Fix | Delete
$rewrapped = self::wrap($toRewrap, $options);
[389] Fix | Delete
$newChunks = explode("\n", $rewrapped);
[390] Fix | Delete
[391] Fix | Delete
$chunks = array_merge($chunks, $newChunks);
[392] Fix | Delete
$wrapped = implode("\n", $chunks);
[393] Fix | Delete
}
[394] Fix | Delete
[395] Fix | Delete
return $wrapped;
[396] Fix | Delete
}
[397] Fix | Delete
[398] Fix | Delete
/**
[399] Fix | Delete
* Unicode and newline aware version of wordwrap.
[400] Fix | Delete
*
[401] Fix | Delete
* @param string $text The text to format.
[402] Fix | Delete
* @param int $width The width to wrap to. Defaults to 72.
[403] Fix | Delete
* @param string $break The line is broken using the optional break parameter. Defaults to '\n'.
[404] Fix | Delete
* @param bool $cut If the cut is set to true, the string is always wrapped at the specified width.
[405] Fix | Delete
* @return string Formatted text.
[406] Fix | Delete
*/
[407] Fix | Delete
public static function wordWrap($text, $width = 72, $break = "\n", $cut = false)
[408] Fix | Delete
{
[409] Fix | Delete
$paragraphs = explode($break, $text);
[410] Fix | Delete
foreach ($paragraphs as &$paragraph) {
[411] Fix | Delete
$paragraph = static::_wordWrap($paragraph, $width, $break, $cut);
[412] Fix | Delete
}
[413] Fix | Delete
[414] Fix | Delete
return implode($break, $paragraphs);
[415] Fix | Delete
}
[416] Fix | Delete
[417] Fix | Delete
/**
[418] Fix | Delete
* Unicode aware version of wordwrap as helper method.
[419] Fix | Delete
*
[420] Fix | Delete
* @param string $text The text to format.
[421] Fix | Delete
* @param int $width The width to wrap to. Defaults to 72.
[422] Fix | Delete
* @param string $break The line is broken using the optional break parameter. Defaults to '\n'.
[423] Fix | Delete
* @param bool $cut If the cut is set to true, the string is always wrapped at the specified width.
[424] Fix | Delete
* @return string Formatted text.
[425] Fix | Delete
*/
[426] Fix | Delete
protected static function _wordWrap($text, $width = 72, $break = "\n", $cut = false)
[427] Fix | Delete
{
[428] Fix | Delete
if ($cut) {
[429] Fix | Delete
$parts = [];
[430] Fix | Delete
while (mb_strlen($text) > 0) {
[431] Fix | Delete
$part = mb_substr($text, 0, $width);
[432] Fix | Delete
$parts[] = trim($part);
[433] Fix | Delete
$text = trim(mb_substr($text, mb_strlen($part)));
[434] Fix | Delete
}
[435] Fix | Delete
[436] Fix | Delete
return implode($break, $parts);
[437] Fix | Delete
}
[438] Fix | Delete
[439] Fix | Delete
$parts = [];
[440] Fix | Delete
while (mb_strlen($text) > 0) {
[441] Fix | Delete
if ($width >= mb_strlen($text)) {
[442] Fix | Delete
$parts[] = trim($text);
[443] Fix | Delete
break;
[444] Fix | Delete
}
[445] Fix | Delete
[446] Fix | Delete
$part = mb_substr($text, 0, $width);
[447] Fix | Delete
$nextChar = mb_substr($text, $width, 1);
[448] Fix | Delete
if ($nextChar !== ' ') {
[449] Fix | Delete
$breakAt = mb_strrpos($part, ' ');
[450] Fix | Delete
if ($breakAt === false) {
[451] Fix | Delete
$breakAt = mb_strpos($text, ' ', $width);
[452] Fix | Delete
}
[453] Fix | Delete
if ($breakAt === false) {
[454] Fix | Delete
$parts[] = trim($text);
[455] Fix | Delete
break;
[456] Fix | Delete
}
[457] Fix | Delete
$part = mb_substr($text, 0, $breakAt);
[458] Fix | Delete
}
[459] Fix | Delete
[460] Fix | Delete
$part = trim($part);
[461] Fix | Delete
$parts[] = $part;
[462] Fix | Delete
$text = trim(mb_substr($text, mb_strlen($part)));
[463] Fix | Delete
}
[464] Fix | Delete
[465] Fix | Delete
return implode($break, $parts);
[466] Fix | Delete
}
[467] Fix | Delete
[468] Fix | Delete
/**
[469] Fix | Delete
* Highlights a given phrase in a text. You can specify any expression in highlighter that
[470] Fix | Delete
* may include the \1 expression to include the $phrase found.
[471] Fix | Delete
*
[472] Fix | Delete
* ### Options:
[473] Fix | Delete
*
[474] Fix | Delete
* - `format` The piece of HTML with that the phrase will be highlighted
[475] Fix | Delete
* - `html` If true, will ignore any HTML tags, ensuring that only the correct text is highlighted
[476] Fix | Delete
* - `regex` A custom regex rule that is used to match words, default is '|$tag|iu'
[477] Fix | Delete
* - `limit` A limit, optional, defaults to -1 (none)
[478] Fix | Delete
*
[479] Fix | Delete
* @param string $text Text to search the phrase in.
[480] Fix | Delete
* @param string|array $phrase The phrase or phrases that will be searched.
[481] Fix | Delete
* @param array $options An array of HTML attributes and options.
[482] Fix | Delete
* @return string The highlighted text
[483] Fix | Delete
* @link https://book.cakephp.org/3/en/core-libraries/text.html#highlighting-substrings
[484] Fix | Delete
*/
[485] Fix | Delete
public static function highlight($text, $phrase, array $options = [])
[486] Fix | Delete
{
[487] Fix | Delete
if (empty($phrase)) {
[488] Fix | Delete
return $text;
[489] Fix | Delete
}
[490] Fix | Delete
[491] Fix | Delete
$defaults = [
[492] Fix | Delete
'format' => '<span class="highlight">\1</span>',
[493] Fix | Delete
'html' => false,
[494] Fix | Delete
'regex' => '|%s|iu',
[495] Fix | Delete
'limit' => -1,
[496] Fix | Delete
];
[497] Fix | Delete
$options += $defaults;
[498] Fix | Delete
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function