PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/monstra/helpers/text.php

https://bitbucket.org/nuarharuha/monstra
PHP | 469 lines | 168 code | 86 blank | 215 comment | 6 complexity | 437219bd0f1e59c97486257fc8f50c75 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php defined('MONSTRA_ACCESS') or die('No direct script access.');
  2. /**
  3. * Text Helper
  4. *
  5. * @package Monstra
  6. * @subpackage Helpers
  7. * @author Romanenko Sergey / Awilum
  8. * @copyright 2012 Romanenko Sergey / Awilum
  9. * @version $Id$
  10. * @since 1.0.0
  11. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  12. * Monstra is free software. This version may have been modified pursuant
  13. * to the GNU General Public License, and as distributed it includes or
  14. * is derivative of works licensed under the GNU General Public License or
  15. * other free or open source software licenses.
  16. * See COPYING.txt for copyright notices and details.
  17. * @filesource
  18. */
  19. class Text {
  20. /**
  21. * Protected constructor since this is a static class.
  22. *
  23. * @access protected
  24. */
  25. protected function __construct() {
  26. // Nothing here
  27. }
  28. /**
  29. * Translit function ua,ru => latin
  30. *
  31. * <code>
  32. * echo Text::translitIt('Привет');
  33. * </code>
  34. *
  35. * @param string $str [ua,ru] string
  36. * @return string $str
  37. */
  38. public static function translitIt($str) {
  39. // Redefine vars
  40. $str = (string) $str;
  41. $patern = array(
  42. "А" => "A", "Б" => "B", "В" => "V", "Г" => "G",
  43. "Д" => "D", "Е" => "E", "Ж" => "J", "З" => "Z",
  44. "И" => "I", "Й" => "Y", "К" => "K", "Л" => "L",
  45. "М" => "M", "Н" => "N", "О" => "O", "П" => "P",
  46. "Р" => "R", "С" => "S", "Т" => "T", "У" => "U",
  47. "Ф" => "F", "Х" => "H", "Ц" => "TS", "Ч" => "CH",
  48. "Ш" => "SH", "Щ" => "SCH", "Ъ" => "", "Ы" => "YI",
  49. "Ь" => "", "Э" => "E", "Ю" => "YU", "Я" => "YA",
  50. "а" => "a", "б" => "b", "в" => "v", "г" => "g",
  51. "д" => "d", "е" => "e", "ж" => "j", "з" => "z",
  52. "и" => "i", "й" => "y", "к" => "k", "л" => "l",
  53. "м" => "m", "н" => "n", "о" => "o","п" => "p",
  54. "р" => "r", "с" => "s", "т" => "t", "у" => "u",
  55. "ф" => "f", "х" => "h", "ц" => "ts", "ч" => "ch",
  56. "ш" => "sh", "щ" => "sch", "ъ" => "y", "ї" => "i",
  57. "Ї" => "Yi", "є" => "ie", "Є" => "Ye", "ы" => "yi",
  58. "ь" => "", "э" => "e", "ю" => "yu", "я" => "ya", "ё" => "yo"
  59. );
  60. return strtr($str, $patern);
  61. }
  62. /**
  63. * Removes any leading and traling slashes from a string
  64. *
  65. * <code>
  66. * echo Text::trimSlashes('some text here/');
  67. * </code>
  68. *
  69. * @param string $str String with slashes
  70. * @return string
  71. */
  72. public static function trimSlashes($str) {
  73. // Redefine vars
  74. $str = (string) $str;
  75. return trim($str, '/');
  76. }
  77. /**
  78. * Removes slashes contained in a string or in an array
  79. *
  80. * <code>
  81. * echo Text::strpSlashes('some \ text \ here');
  82. * </code>
  83. *
  84. * @param string $str String with slashes
  85. * @return string
  86. */
  87. public static function strpSlashes($str) {
  88. // Redefine vars
  89. $str = (string) $str;
  90. if (is_array($str)) {
  91. foreach ($str as $key => $val) {
  92. $str[$key] = stripslashes($val);
  93. }
  94. } else {
  95. $str = stripslashes($str);
  96. }
  97. return $str;
  98. }
  99. /**
  100. * Removes single and double quotes from a string
  101. *
  102. * <code>
  103. * echo Text::stripQuotes('some "text" here');
  104. * </code>
  105. *
  106. * @param string $str String with single and double quotes
  107. * @return string
  108. */
  109. public static function stripQuotes($str) {
  110. // Redefine vars
  111. $str = (string) $str;
  112. return str_replace(array('"', "'"), '', $str);
  113. }
  114. /**
  115. * Convert single and double quotes to entities
  116. *
  117. * <code>
  118. * echo Text::quotesToEntities('some "text" here');
  119. * </code>
  120. *
  121. * @param string $str String with single and double quotes
  122. * @return string
  123. */
  124. public static function quotesToEntities($str) {
  125. // Redefine vars
  126. $str = (string) $str;
  127. return str_replace(array("\'", "\"", "'", '"'), array("&#39;", "&quot;", "&#39;", "&quot;"), $str);
  128. }
  129. /**
  130. * Creates a random string of characters
  131. *
  132. * <code>
  133. * echo Text::random();
  134. * </code>
  135. *
  136. * @param string $type The type of string. Default is 'alnum'
  137. * @param integer $length The number of characters. Default is 16
  138. * @return string
  139. */
  140. public static function random($type = 'alnum', $length = 16) {
  141. // Redefine vars
  142. $type = (string) $type;
  143. $length = (int) $length;
  144. switch($type) {
  145. case 'basic':
  146. return mt_rand();
  147. break;
  148. default:
  149. case 'alnum':
  150. case 'numeric':
  151. case 'nozero':
  152. case 'alpha':
  153. case 'distinct':
  154. case 'hexdec':
  155. switch ($type) {
  156. case 'alpha':
  157. $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  158. break;
  159. default:
  160. case 'alnum':
  161. $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  162. break;
  163. case 'numeric':
  164. $pool = '0123456789';
  165. break;
  166. case 'nozero':
  167. $pool = '123456789';
  168. break;
  169. case 'distinct':
  170. $pool = '2345679ACDEFHJKLMNPRSTUVWXYZ';
  171. break;
  172. case 'hexdec':
  173. $pool = '0123456789abcdef';
  174. break;
  175. }
  176. $str = '';
  177. for ($i=0; $i < $length; $i++) {
  178. $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
  179. }
  180. return $str;
  181. break;
  182. case 'unique':
  183. return md5(uniqid(mt_rand()));
  184. break;
  185. case 'sha1' :
  186. return sha1(uniqid(mt_rand(), true));
  187. break;
  188. }
  189. }
  190. /**
  191. * Cut string
  192. *
  193. * <code>
  194. * echo Text::cut('Some text here', 5);
  195. * </code>
  196. *
  197. * @param string $str Input string
  198. * @param integer $length Length after cut
  199. * @param string $cut_msg Message after cut string
  200. * @return string
  201. */
  202. public static function cut($str, $length, $cut_msg = null) {
  203. // Redefine vars
  204. $str = (string) $str;
  205. $length = (int) $length;
  206. if (isset($cut_msg)) $msg = $cut_msg; else $msg = '...';
  207. return function_exists('mb_substr') ? mb_substr($str, 0, $length, 'utf-8') . $msg : substr($str, 0, $length) . $msg;
  208. }
  209. /**
  210. * Lowercase
  211. *
  212. * <code>
  213. * echo Text::lowercase('Some text here');
  214. * </code>
  215. *
  216. * @param string $str String
  217. * @return string
  218. */
  219. public static function lowercase($str) {
  220. // Redefine vars
  221. $str = (string) $str;
  222. return function_exists('mb_strtolower') ? mb_strtolower($str, 'utf-8') : strtolower($str);
  223. }
  224. /**
  225. * Uppercase
  226. *
  227. * <code>
  228. * echo Text::uppercase('some text here');
  229. * </code>
  230. *
  231. * @param string $str String
  232. * @return string
  233. */
  234. public static function uppercase($str) {
  235. // Redefine vars
  236. $str = (string) $str;
  237. return function_exists('mb_strtoupper') ? mb_strtoupper($str, 'utf-8') : strtoupper($str);
  238. }
  239. /**
  240. * Get length
  241. *
  242. * <code>
  243. * echo Text::length('Some text here');
  244. * </code>
  245. *
  246. * @param string $str String
  247. * @return string
  248. */
  249. public static function length($str) {
  250. // Redefine vars
  251. $str = (string) $str;
  252. return function_exists('mb_strlen') ? mb_strlen($str, 'utf-8') : strlen($str);
  253. }
  254. /**
  255. * Create a lorem ipsum text
  256. *
  257. * <code>
  258. * echo Text::lorem(2);
  259. * </code>
  260. *
  261. * @param integer $num Count
  262. * @return string
  263. */
  264. public static function lorem($num = 1) {
  265. // Redefine vars
  266. $num = (int) $num;
  267. return str_repeat('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.', (int)$num);
  268. }
  269. /**
  270. * Extract the last `$num` characters from a string.
  271. *
  272. * <code>
  273. * echo Text::right('Some text here', 4);
  274. * </code>
  275. *
  276. * @param string $str The string to extract the characters from.
  277. * @param integer $num The number of characters to extract.
  278. * @return string
  279. */
  280. public static function right($str, $num){
  281. // Redefine vars
  282. $str = (string) $str;
  283. $num = (int) $num;
  284. return substr($str, Text::length($str)-$num, $num);
  285. }
  286. /**
  287. * Extract the first `$num` characters from a string.
  288. *
  289. * <code>
  290. * echo Text::left('Some text here', 4);
  291. * </code>
  292. *
  293. * @param string $str The string to extract the characters from.
  294. * @param integer $num The number of characters to extract.
  295. * @return string
  296. */
  297. public static function left($str, $num){
  298. // Redefine vars
  299. $str = (string) $str;
  300. $num = (int) $num;
  301. return substr($str, 0, $num);
  302. }
  303. /**
  304. * Replaces newline with <br> or <br />.
  305. *
  306. * <code>
  307. * echo Text::nl2br("Some \n text \n here");
  308. * </code>
  309. *
  310. * @param string $str The input string
  311. * @return string
  312. */
  313. public static function nl2br($str, $xhtml = true) {
  314. // Redefine vars
  315. $str = (string) $str;
  316. $xhtml = (bool) $xhtml;
  317. return str_replace(array("\r\n", "\n\r", "\n", "\r"), (($xhtml) ? '<br />' : '<br>'), $str);
  318. }
  319. /**
  320. * Replaces <br> and <br /> with newline.
  321. *
  322. * <code>
  323. * echo Text::br2nl("Some <br /> text <br /> here");
  324. * </code>
  325. *
  326. * @param string $str The input string
  327. * @return string
  328. */
  329. public static function br2nl($str) {
  330. // Redefine vars
  331. $str = (string) $str;
  332. return str_replace(array('<br>', '<br/>', '<br />'), "\n", $str);
  333. }
  334. /**
  335. * Converts & to &amp;.
  336. *
  337. * <code>
  338. * echo Text::ampEncode("T&CMS");
  339. * </code>
  340. *
  341. * @param string $str The input string
  342. * @return string
  343. */
  344. public static function ampEncode($str) {
  345. // Redefine vars
  346. $str = (string) $str;
  347. return str_replace('&', '&amp;', $str);
  348. }
  349. /**
  350. * Converts &amp; to &.
  351. *
  352. * <code>
  353. * echo Text::ampEncode("T&amp;CMS");
  354. * </code>
  355. *
  356. * @param string $str The input string
  357. * @return string
  358. */
  359. public static function ampDecode($str) {
  360. // Redefine vars
  361. $str = (string) $str;
  362. return str_replace('&amp;', '&', $str);
  363. }
  364. /**
  365. * Convert plain text to html
  366. *
  367. * <code>
  368. * echo Text::toHtml('test');
  369. * </code>
  370. *
  371. * @param string $str String
  372. * @return string
  373. */
  374. public static function toHtml($str) {
  375. // Redefine vars
  376. $str = (string) $str;
  377. return html_entity_decode($str, ENT_QUOTES, 'utf-8');
  378. }
  379. }