TextLib.php 9.7 KB

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