TextLib.php 9.8 KB

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