PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/helpers/text.php

https://github.com/rii-J/concrete5-de
PHP | 301 lines | 156 code | 30 blank | 115 comment | 26 complexity | 9994da3f42cb1971c996e37535328dc1 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Helpers
  4. * @category Concrete
  5. * @author Andrew Embler <andrew@concrete5.org>
  6. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  7. * @license http://www.concrete5.org/license/ MIT License
  8. */
  9. /**
  10. * Functions useful for working with text.
  11. * @package Helpers
  12. * @category Concrete
  13. * @author Andrew Embler <andrew@concrete5.org>
  14. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  15. * @license http://www.concrete5.org/license/ MIT License
  16. */
  17. defined('C5_EXECUTE') or die("Access Denied.");
  18. class TextHelper {
  19. /**
  20. * Takes text and returns it in the "lowercase-and-dashed-with-no-punctuation" format
  21. * @param string $handle
  22. * @param bool $leaveSlashes
  23. * @return string $handle
  24. */
  25. function sanitizeFileSystem($handle, $leaveSlashes=false) {
  26. $handle = trim($handle);
  27. $handle = str_replace(PAGE_PATH_SEPARATOR, '-', $handle);
  28. $searchMulti = array(
  29. "ä",
  30. "ö",
  31. "ß",
  32. "ü",
  33. "æ",
  34. "ø",
  35. "å",
  36. "é",
  37. "è"
  38. );
  39. $replaceMulti = array(
  40. 'ae',
  41. 'oe',
  42. 'ss',
  43. 'ue',
  44. 'ae',
  45. 'oe',
  46. 'aa',
  47. 'e',
  48. 'e'
  49. );
  50. $handle = str_replace($searchMulti, $replaceMulti, $handle);
  51. $searchNormal = array("/[&]/", "/[\s]+/", "/[^0-9A-Za-z-_.]/", "/-+/");
  52. $searchSlashes = array("/[&]/", "/[\s]+/", "/[^0-9A-Za-z-_.\/]/", "/-+/");
  53. $replace = array("and", "-", "", "-");
  54. $search = $searchNormal;
  55. if ($leaveSlashes) {
  56. $search = $searchSlashes;
  57. }
  58. $handle = preg_replace($search, $replace, $handle);
  59. if (function_exists('mb_strtolower')) {
  60. $handle = mb_strtolower($handle, APP_CHARSET);
  61. } else {
  62. $handle = strtolower($handle);
  63. }
  64. $handle = trim($handle, '-');
  65. $handle = str_replace('-', PAGE_PATH_SEPARATOR, $handle);
  66. return $handle;
  67. }
  68. /**
  69. * Strips tags and optionally reduces string to specified length.
  70. * @param string $string
  71. * @param int $maxlength
  72. * @param string $allowed
  73. * @return string
  74. */
  75. function sanitize($string, $maxlength = 0, $allowed = '') {
  76. $text = trim(strip_tags($string, $allowed));
  77. if ($maxlength > 0) {
  78. if (function_exists('mb_substr')) {
  79. $text = mb_substr($text, 0, $maxlength, APP_CHARSET);
  80. } else {
  81. $text = substr($text, 0, $maxlength);
  82. }
  83. }
  84. if ($text == null) {
  85. return ""; // we need to explicitly return a string otherwise some DB functions might insert this as a ZERO.
  86. }
  87. return $text;
  88. }
  89. /**
  90. * Leaves only characters that are valid in email addresses (RFC)
  91. * @param string $email
  92. * @return string
  93. */
  94. public function email($email) {
  95. $regex = "/[^a-zA-Z0-9_\.!#\$\&'\*\+-?^`{|}~@]/i";
  96. return preg_replace($regex, '', $email);
  97. }
  98. /**
  99. * always use in place of htmlentites(), so it works with different langugages
  100. * @param string $v
  101. * @return string
  102. */
  103. public function entities($v){
  104. return htmlentities( $v, ENT_COMPAT, APP_CHARSET);
  105. }
  106. /**
  107. * An alias for shorten()
  108. * @param string $textStr
  109. * @param int $numChars
  110. * @param string $tail
  111. * @return string
  112. */
  113. public function shorten($textStr, $numChars = 255, $tail = '…') {
  114. return $this->shortText($textStr, $numChars, $tail);
  115. }
  116. /**
  117. * Like sanitize, but requiring a certain number characters, and assuming a tail
  118. * @param string $textStr
  119. * @param int $numChars
  120. * @param string $tail
  121. * @return string $textStr
  122. */
  123. function shortText($textStr, $numChars=255, $tail='…') {
  124. if (intval($numChars)==0) $numChars=255;
  125. $textStr=strip_tags($textStr);
  126. if (function_exists('mb_substr') && function_exists('mb_strlen')) {
  127. if (mb_strlen($textStr, APP_CHARSET) > $numChars) {
  128. $textStr = mb_substr($textStr, 0, $numChars, APP_CHARSET) . $tail;
  129. }
  130. } else {
  131. if (strlen($textStr) > $numChars) {
  132. $textStr = substr($textStr, 0, $numChars) . $tail;
  133. }
  134. }
  135. return $textStr;
  136. }
  137. /**
  138. * Shortens and sanitizes a string but only cuts at word boundaries
  139. * @param string $textStr
  140. * @param int $numChars
  141. * @param string $tail
  142. */
  143. function shortenTextWord($textStr, $numChars=255, $tail='…') {
  144. if (intval($numChars)==0) $numChars=255;
  145. $textStr=strip_tags($textStr);
  146. if (function_exists('mb_substr')) {
  147. if (mb_strlen($textStr, APP_CHARSET) > $numChars) {
  148. $textStr=preg_replace('/\s+?(\S+)?$/', '', mb_substr($textStr, 0, $numChars + 1, APP_CHARSET)) . $tail;
  149. }
  150. } else {
  151. if (strlen($textStr) > $numChars) {
  152. $textStr = preg_replace('/\s+?(\S+)?$/', '', substr($textStr, 0, $numChars + 1)) . $tail;
  153. }
  154. }
  155. return $textStr;
  156. }
  157. /**
  158. * Takes a string and turns it into the CamelCase or StudlyCaps version
  159. * @param string $string
  160. * @return string
  161. */
  162. public function camelcase($string) {
  163. return Object::camelcase($string);
  164. }
  165. /**
  166. * Scans passed text and automatically hyperlinks any URL inside it
  167. * @param string $input
  168. * @param int $newWindow
  169. * @return string $output
  170. */
  171. public function autolink($input,$newWindow=0) {
  172. $target=($newWindow)?' target="_blank" ':'';
  173. $output = preg_replace("/(http:\/\/|https:\/\/|(www\.))(([^\s<]{4,80})[^\s<]*)/", '<a href="http://$2$3" '.$target.' rel="nofollow">http://$2$4</a>', $input);
  174. return ($output);
  175. }
  176. /**
  177. * automatically add hyperlinks to any twitter style @usernames in a string
  178. * @param string $input
  179. * @param int $newWindow
  180. * @param int $withSearch
  181. * @return string $output
  182. */
  183. public function twitterAutolink($input,$newWindow=0,$withSearch=0) {
  184. $target=($newWindow)?' target="_blank" ':'';
  185. $output = preg_replace('/([\.|\,|\:|\¡|\¿|\>|\{|\(]?)@{1}(\w*)([\.|\,|\:|\!|\?|\>|\}|\)]?)\s/i', "$1<a href=\"http://twitter.com/$2\" ".$target." class=\"twitter-username\">@$2</a>$3 ", $input);
  186. if($withSearch)
  187. $output = preg_replace('/([\.|\,|\:|\¡|\¿|\>|\{|\(]?)#{1}(\w*)([\.|\,|\:|\!|\?|\>|\}|\)]?)\s/i', "$1<a href=\"http://search.twitter.com/search?q=%23$2\" ".$target." class=\"twitter-search\">#$2</a>$3 ", $input);
  188. return $output;
  189. }
  190. /**
  191. * Runs a number of text functions, including autolink, nl2br, strip_tags. Assumes that you want simple
  192. * text comments but witih a few niceties.
  193. * @param string $input
  194. * @return string $output
  195. */
  196. public function makenice($input) {
  197. $output = strip_tags($input);
  198. $output = $this->autolink($output);
  199. $output = nl2br($output);
  200. return $output;
  201. }
  202. /**
  203. * A wrapper for PHP's fnmatch() function, which some installations don't have.
  204. * @param string $pattern
  205. * @param string $string
  206. * @return bool
  207. */
  208. public function fnmatch($pattern, $string) {
  209. if(!function_exists('fnmatch')) {
  210. return preg_match("#^".strtr(preg_quote($pattern, '#'), array('\*' => '.*', '\?' => '.', '\[' => '[', '\]' => ']'))."$#i", $string);
  211. } else {
  212. return fnmatch($pattern, $string);
  213. }
  214. }
  215. /**
  216. * Takes a CamelCase string and turns it into camel_case
  217. * @param string $string
  218. * @return string
  219. */
  220. public function uncamelcase($string) {
  221. $v = preg_split('/([A-Z])/', $string, false, PREG_SPLIT_DELIM_CAPTURE);
  222. $a = array();
  223. array_shift($v);
  224. for($i = 0; $i < count($v); $i++) {
  225. if ($i % 2) {
  226. if (function_exists('mb_strtolower')) {
  227. $a[] = mb_strtolower($v[$i - 1] . $v[$i], APP_CHARSET);
  228. } else {
  229. $a[] = strtolower($v[$i - 1] . $v[$i]);
  230. }
  231. }
  232. }
  233. return implode('_', $a);
  234. }
  235. /**
  236. * Takes a handle-based string like "blah_blah" or "blah-blah" or "blah/blah" and turns it into "Blah Blah"
  237. * @param string $string
  238. * @return string $r1
  239. */
  240. public function unhandle($string) {
  241. $r1 = ucwords(str_replace(array('_', '-', '/'), ' ', $string));
  242. return $r1;
  243. }
  244. /**
  245. * An alias for sanitizeFileSystem()
  246. * @param string $handle
  247. * @param bool $leaveSlashes
  248. * @return string $handle
  249. */
  250. public function handle($handle, $leaveSlashes=false) {
  251. return $this->sanitizeFileSystem($handle, $leaveSlashes=false);
  252. }
  253. /**
  254. * Strips out non-alpha-numeric characters
  255. * @param string $val
  256. * @return string
  257. */
  258. public function filterNonAlphaNum($val){
  259. return preg_replace('/[^[:alnum:]]/', '', $val);
  260. }
  261. /**
  262. * Highlights a string within a string with the class ccm-hightlight-search
  263. * @param string $value
  264. * @param string $searchString
  265. * @return string
  266. */
  267. public function highlightSearch($value, $searchString) {
  268. return str_ireplace($searchString, '<em class="ccm-highlight-search">' . $searchString . '</em>', $value);
  269. }
  270. }
  271. ?>