text = $text; } /** * Count words in a text. * * //TODO use str_word_count() instead!!! * * @param string $text * @return int * 2009-11-11 ms */ public static function numberOfWords($text) { $count = 0; $words = explode(' ', $text); foreach ($words as $word) { $word = trim($word); if (!empty($word)) { $count++; } } return $count; } /** * Return an abbreviated string, with characters in the middle of the * excessively long string replaced by $ending. * * @param string $text The original string. * @param integer $length The length at which to abbreviate. * @return string The abbreviated string, if longer than $length. */ public static function abbreviate($text, $length = 20, $ending = '...') { return (mb_strlen($text) > $length) ? rtrim(mb_substr($text, 0, round(($length - 3) / 2))) . $ending . ltrim(mb_substr($text, (($length - 3) / 2) * -1)) : $text; } /* other */ public function convertToOrd($str = null, $separator = '-') { /* if (!class_exists('UnicodeLib')) { App::uses('UnicodeLib', 'Tools.Lib'); } */ if ($str === null) { $str = $this->text; } $chars = preg_split('//', $str, -1); $res = array(); foreach ($chars as $char) { //$res[] = UnicodeLib::ord($char); $res[] = ord($char); } return implode($separator, $res); } public static function convertToOrdTable($str, $maxCols = 20) { $res = '
| '.implode(' | ', $r['chr']).' | '; $res .= '
|---|---|
| '.implode(' | ', $r['ord']).' |
* // Returns "This is a..."
* echo TextExt::maxWords('This is a sentence.', 3);
*
* // Limit the number of words and append a custom ending
* echo Str::words('This is a sentence.', 3, '---');
*
*
* @param string $value
* @param int $words
* @param array $options
* - ellipsis
* - html
* @return string
*/
public static function maxWords($value, $words = 100, $options = array()) {
$default = array(
'ellipsis' => '...'
);
if (!empty($options['html']) && Configure::read('App.encoding') === 'UTF-8') {
$default['ellipsis'] = "\xe2\x80\xa6";
}
$options = array_merge($default, $options);
if (trim($value) === '') {
return '';
}
preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/u', $value, $matches);
$end = $options['ellipsis'];
if (mb_strlen($value) === mb_strlen($matches[0])) {
$end = '';
}
return rtrim($matches[0]) . $end;
}
/**
* High ASCII to Entities
*
* Converts High ascii text and MS Word special characters to character entities
*
* @param string
* @return string
*/
public function ascii_to_entities($str) {
$count = 1;
$out = '';
$temp = array();
for ($i = 0, $s = strlen($str); $i < $s; $i++) {
$ordinal = ord($str[$i]);
if ($ordinal < 128) {
/*
If the $temp array has a value but we have moved on, then it seems only
fair that we output that entity and restart $temp before continuing. -Paul
*/
if (count($temp) == 1) {
$out .= '' . array_shift($temp) . ';';
$count = 1;
}
$out .= $str[$i];
} else {
if (count($temp) == 0) {
$count = ($ordinal < 224) ? 2 : 3;
}
$temp[] = $ordinal;
if (count($temp) == $count) {
$number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] %
64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);
$out .= '' . $number . ';';
$count = 1;
$temp = array();
}
}
}
return $out;
}
// ------------------------------------------------------------------------
/**
* Entities to ASCII
*
* Converts character entities back to ASCII
*
* @param string
* @param bool
* @return string
*/
public function entities_to_ascii($str, $all = true) {
if (preg_match_all('/\(\d+)\;/', $str, $matches)) {
for ($i = 0, $s = count($matches['0']); $i < $s; $i++) {
$digits = $matches['1'][$i];
$out = '';
if ($digits < 128) {
$out .= chr($digits);
} elseif ($digits < 2048) {
$out .= chr(192 + (($digits - ($digits % 64)) / 64));
$out .= chr(128 + ($digits % 64));
} else {
$out .= chr(224 + (($digits - ($digits % 4096)) / 4096));
$out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64));
$out .= chr(128 + ($digits % 64));
}
$str = str_replace($matches['0'][$i], $out, $str);
}
}
if ($all) {
$str = str_replace(array("&", "<", ">", """, "'", "-"),
array("&", "<", ">", "\"", "'", "-"), $str);
}
return $str;
}
/**
* Reduce Double Slashes
*
* Converts double slashes in a string to a single slash,
* except those found in http://
*
* http://www.some-site.com//index.php
*
* becomes:
*
* http://www.some-site.com/index.php
*
* @param string
* @return string
*/
public function reduce_double_slashes($str) {
return preg_replace("#([^:])//+#", "\\1/", $str);
}
// ------------------------------------------------------------------------
/**
* Reduce Multiples
*
* Reduces multiple instances of a particular character. Example:
*
* Fred, Bill,, Joe, Jimmy
*
* becomes:
*
* Fred, Bill, Joe, Jimmy
*
* @param string
* @param string the character you wish to reduce
* @param bool TRUE/FALSE - whether to trim the character from the beginning/end
* @return string
*/
public function reduce_multiples($str, $character = ',', $trim = false) {
$str = preg_replace('#' . preg_quote($character, '#') . '{2,}#', $character, $str);
if ($trim === true) {
$str = trim($str, $character);
}
return $str;
}
}