Text.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <?php
  2. namespace Tools\Utility;
  3. use Cake\Core\Configure;
  4. use Cake\Utility\Text as CakeText;
  5. /**
  6. * Extends CakeText.
  7. * //TODO: cleanup
  8. */
  9. class Text extends CakeText {
  10. /**
  11. * @var string
  12. */
  13. public $text;
  14. /**
  15. * @var int
  16. */
  17. public $length;
  18. /**
  19. * @var string
  20. */
  21. public $char;
  22. /**
  23. * @var string
  24. */
  25. public $letter;
  26. /**
  27. * @var string
  28. */
  29. public $space;
  30. /**
  31. * @var string
  32. */
  33. public $word;
  34. /**
  35. * @var string
  36. */
  37. public $rWord;
  38. /**
  39. * @var string
  40. */
  41. public $sen;
  42. /**
  43. * @var string
  44. */
  45. public $rSen;
  46. /**
  47. * @var string
  48. */
  49. public $para;
  50. /**
  51. * @var string
  52. */
  53. public $rPara;
  54. /**
  55. * @var string
  56. */
  57. public $beautified;
  58. /**
  59. * Read tab data (tab-separated data).
  60. *
  61. * @param string $text
  62. * @return array
  63. */
  64. public function readTab($text) {
  65. $pieces = explode("\n", $text);
  66. $result = [];
  67. foreach ($pieces as $piece) {
  68. $tmp = explode("\t", trim($piece, "\r\n"));
  69. $result[] = $tmp;
  70. }
  71. return $result;
  72. }
  73. /**
  74. * Read with a specific pattern.
  75. *
  76. * E.g.: '%s,%s,%s'
  77. *
  78. * @param string $text
  79. * @param string $pattern
  80. * @return array
  81. */
  82. public function readWithPattern($text, $pattern) {
  83. $pieces = explode("\n", $text);
  84. $result = [];
  85. foreach ($pieces as $piece) {
  86. $result[] = sscanf(trim($piece, "\r\n"), $pattern);
  87. }
  88. return $result;
  89. }
  90. /**
  91. * Count words in a text.
  92. *
  93. * //TODO use str_word_count() instead!!!
  94. *
  95. * @param string $text
  96. * @return int
  97. */
  98. public static function numberOfWords($text) {
  99. $count = 0;
  100. $words = explode(' ', $text);
  101. foreach ($words as $word) {
  102. $word = trim($word);
  103. if (!empty($word)) {
  104. $count++;
  105. }
  106. }
  107. return $count;
  108. }
  109. /**
  110. * Count chars in a text.
  111. *
  112. * Options:
  113. * - 'whitespace': If whitespace should be counted, as well, defaults to false
  114. *
  115. * @param string $text
  116. * @param array $options
  117. * @return int
  118. */
  119. public static function numberOfChars($text, array $options = []) {
  120. $text = str_replace(["\r", "\n", "\t", ' '], '', $text);
  121. $count = mb_strlen($text);
  122. return $count;
  123. }
  124. /**
  125. * Return an abbreviated string, with characters in the middle of the
  126. * excessively long string replaced by $ending.
  127. *
  128. * @param string $text The original string.
  129. * @param int $length The length at which to abbreviate.
  130. * @param string $ending Defaults to ...
  131. * @return string The abbreviated string, if longer than $length.
  132. */
  133. public static function abbreviate($text, $length = 20, $ending = '...') {
  134. if (mb_strlen($text) <= $length) {
  135. return $text;
  136. }
  137. return rtrim(mb_substr($text, 0, round(($length - 3) / 2))) . $ending . ltrim(mb_substr($text, (($length - 3) / 2) * -1));
  138. }
  139. /**
  140. * TextLib::convertToOrd()
  141. *
  142. * @param string $str
  143. * @param string $separator
  144. * @return string
  145. */
  146. public function convertToOrd($str, $separator = '-') {
  147. $chars = preg_split('//', $str, -1);
  148. $res = [];
  149. foreach ($chars as $char) {
  150. //$res[] = UnicodeLib::ord($char);
  151. $res[] = ord($char);
  152. }
  153. return implode($separator, $res);
  154. }
  155. /**
  156. * @param string $str
  157. * @param int $maxCols
  158. * @return string
  159. */
  160. public static function convertToOrdTable($str, $maxCols = 20) {
  161. $res = '<table>';
  162. $r = ['chr' => [], 'ord' => []];
  163. $chars = preg_split('//', $str, -1);
  164. $count = 0;
  165. foreach ($chars as $key => $char) {
  166. if ($maxCols && $maxCols < $count || $key === count($chars) - 1) {
  167. $res .= '<tr><th>' . implode('</th><th>', $r['chr']) . '</th>';
  168. $res .= '</tr>';
  169. $res .= '<tr>';
  170. $res .= '<td>' . implode('</th><th>', $r['ord']) . '</td></tr>';
  171. $count = 0;
  172. $r = ['chr' => [], 'ord' => []];
  173. }
  174. $count++;
  175. //$res[] = UnicodeLib::ord($char);
  176. $r['ord'][] = ord($char);
  177. $r['chr'][] = $char;
  178. }
  179. $res .= '</table>';
  180. return $res;
  181. }
  182. /**
  183. * Explode a string of given tags into an array.
  184. *
  185. * @param string $tags
  186. * @return array
  187. */
  188. public function explodeTags($tags) {
  189. // This regexp allows the following types of user input:
  190. // this, "somecompany, llc", "and ""this"" w,o.rks", foo bar
  191. $regexp = '%(?:^|,\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x';
  192. preg_match_all($regexp, $tags, $matches);
  193. $typedTags = array_unique($matches[1]);
  194. $tags = [];
  195. foreach ($typedTags as $tag) {
  196. // If a user has escaped a term (to demonstrate that it is a group,
  197. // or includes a comma or quote character), we remove the escape
  198. // formatting so to save the term into the database as the user intends.
  199. $tag = trim(str_replace('""', '"', preg_replace('/^"(.*)"$/', '\1', $tag)));
  200. if ($tag) {
  201. $tags[] = $tag;
  202. }
  203. }
  204. return $tags;
  205. }
  206. /**
  207. * Implode an array of tags into a string.
  208. *
  209. * @param array $tags
  210. * @return string
  211. */
  212. public function implodeTags(array $tags) {
  213. $encodedTags = [];
  214. foreach ($tags as $tag) {
  215. // Commas and quotes in tag names are special cases, so encode them.
  216. if (strpos($tag, ',') !== false || strpos($tag, '"') !== false) {
  217. $tag = '"' . str_replace('"', '""', $tag) . '"';
  218. }
  219. $encodedTags[] = $tag;
  220. }
  221. return implode(', ', $encodedTags);
  222. }
  223. /**
  224. * Prevents [widow words](http://www.shauninman.com/archive/2006/08/22/widont_wordpress_plugin)
  225. * by inserting a non-breaking space between the last two words.
  226. *
  227. * echo Text::widont($text);
  228. *
  229. * @param string $str Text to remove widows from
  230. * @return string
  231. */
  232. public function widont($str) {
  233. $str = rtrim($str);
  234. $space = strrpos($str, ' ');
  235. if ($space !== false) {
  236. $str = substr($str, 0, $space) . '&nbsp;' . substr($str, $space + 1);
  237. }
  238. return $str;
  239. }
  240. /* text object specific */
  241. /**
  242. * Extract words
  243. *
  244. * @param string $text
  245. * @param array $options
  246. * - min_char, max_char, case_sensititive, ...
  247. * @return array
  248. */
  249. public function words($text, array $options = []) {
  250. $text = str_replace([PHP_EOL, "\t"], ' ', $text);
  251. $pieces = explode(' ', $text);
  252. $pieces = array_unique($pieces);
  253. // strip chars like . or ,
  254. foreach ($pieces as $key => $piece) {
  255. if (empty($options['case_sensitive'])) {
  256. $piece = mb_strtolower($piece);
  257. }
  258. $search = [',', '.', ';', ':', '#', '', '(', ')', '{', '}', '[', ']', '$', '%', '"', '!', '?', '<', '>', '=', '/'];
  259. $search = array_merge($search, [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]);
  260. $piece = str_replace($search, '', $piece);
  261. $piece = trim($piece);
  262. if (empty($piece) || !empty($options['min_char']) && mb_strlen($piece) < $options['min_char'] || !empty($options['max_char']) && mb_strlen($piece) > $options['max_char']) {
  263. unset($pieces[$key]);
  264. } else {
  265. $pieces[$key] = $piece;
  266. }
  267. }
  268. $pieces = array_unique($pieces);
  269. return $pieces;
  270. }
  271. /**
  272. * Limit the number of words in a string.
  273. *
  274. * <code>
  275. * // Returns "This is a..."
  276. * echo TextExt::maxWords('This is a sentence.', 3);
  277. *
  278. * // Limit the number of words and append a custom ending
  279. * echo Str::words('This is a sentence.', 3, '---');
  280. * </code>
  281. *
  282. * @param string $value
  283. * @param int $words
  284. * @param array $options
  285. * - ellipsis
  286. * - html
  287. * @return string
  288. */
  289. public static function maxWords($value, $words = 100, array $options = []) {
  290. $defaults = [
  291. 'ellipsis' => '...'
  292. ];
  293. if (!empty($options['html']) && Configure::read('App.encoding') === 'UTF-8') {
  294. $defaults['ellipsis'] = "\xe2\x80\xa6";
  295. }
  296. $options += $defaults;
  297. if (trim($value) === '') {
  298. return '';
  299. }
  300. preg_match('/^\s*+(?:\S++\s*+){1,' . $words . '}/u', $value, $matches);
  301. $end = $options['ellipsis'];
  302. if (mb_strlen($value) === mb_strlen($matches[0])) {
  303. $end = '';
  304. }
  305. return rtrim($matches[0]) . $end;
  306. }
  307. /**
  308. * High ASCII to Entities
  309. *
  310. * Converts High ascii text and MS Word special characters to character entities
  311. *
  312. * @param string $str
  313. * @return string
  314. */
  315. public function asciiToEntities($str) {
  316. $count = 1;
  317. $out = '';
  318. $temp = [];
  319. for ($i = 0, $s = strlen($str); $i < $s; $i++) {
  320. $ordinal = ord($str[$i]);
  321. if ($ordinal < 128) {
  322. /*
  323. If the $temp array has a value but we have moved on, then it seems only
  324. fair that we output that entity and restart $temp before continuing. -Paul
  325. */
  326. if (count($temp) == 1) {
  327. $out .= '&#' . array_shift($temp) . ';';
  328. $count = 1;
  329. }
  330. $out .= $str[$i];
  331. } else {
  332. if (count($temp) == 0) {
  333. $count = ($ordinal < 224) ? 2 : 3;
  334. }
  335. $temp[] = $ordinal;
  336. if (count($temp) == $count) {
  337. $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] %
  338. 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);
  339. $out .= '&#' . $number . ';';
  340. $count = 1;
  341. $temp = [];
  342. }
  343. }
  344. }
  345. return $out;
  346. }
  347. /**
  348. * Entities to ASCII
  349. *
  350. * Converts character entities back to ASCII
  351. *
  352. * @param string $str
  353. * @param bool $all
  354. * @return string
  355. */
  356. public function entitiesToAscii($str, $all = true) {
  357. if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) {
  358. for ($i = 0, $s = count($matches['0']); $i < $s; $i++) {
  359. $digits = $matches['1'][$i];
  360. $out = '';
  361. if ($digits < 128) {
  362. $out .= chr($digits);
  363. } elseif ($digits < 2048) {
  364. $out .= chr(192 + (($digits - ($digits % 64)) / 64));
  365. $out .= chr(128 + ($digits % 64));
  366. } else {
  367. $out .= chr(224 + (($digits - ($digits % 4096)) / 4096));
  368. $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64));
  369. $out .= chr(128 + ($digits % 64));
  370. }
  371. $str = str_replace($matches['0'][$i], $out, $str);
  372. }
  373. }
  374. if ($all) {
  375. $str = str_replace(['&amp;', '&lt;', '&gt;', '&quot;', '&apos;', '&#45;'],
  376. ['&', '<', '>', '"', "'", '-'], $str);
  377. }
  378. return $str;
  379. }
  380. /**
  381. * Reduce Double Slashes
  382. *
  383. * Converts double slashes in a string to a single slash,
  384. * except those found in http://
  385. *
  386. * http://www.some-site.com//index.php
  387. *
  388. * becomes:
  389. *
  390. * http://www.some-site.com/index.php
  391. *
  392. * @param string $str
  393. * @return string
  394. */
  395. public function reduce_double_slashes($str) {
  396. return preg_replace('#([^:])//+#', '\\1/', $str);
  397. }
  398. }