PageRenderTime 60ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/zan/helpers/string.php

https://github.com/triartdesign/ZanPHP
PHP | 617 lines | 463 code | 73 blank | 81 comment | 59 complexity | db8465b8066871fbebed7bbe784dd8c0 MD5 | raw file
  1. <?php
  2. /**
  3. * ZanPHP
  4. *
  5. * An open source agile and rapid development framework for PHP 5
  6. *
  7. * @package ZanPHP
  8. * @author MilkZoft Developer Team
  9. * @copyright Copyright (c) 2011, MilkZoft, Inc.
  10. * @license http://www.zanphp.com/documentation/en/license/
  11. * @link http://www.zanphp.com
  12. * @version 1.0
  13. */
  14. /**
  15. * Access from index.php:
  16. */
  17. if(!defined("_access")) {
  18. die("Error: You don't have permission to access here...");
  19. }
  20. /**
  21. * String Helper
  22. *
  23. *
  24. *
  25. * @package ZanPHP
  26. * @subpackage core
  27. * @category helpers
  28. * @author MilkZoft Developer Team
  29. * @link http://www.zanphp.com/documentation/en/helpers/string_helper
  30. */
  31. function bbCode($HTML) {
  32. $a = array(
  33. "/\[Video: (.*?)\]/is"
  34. );
  35. $b = array(
  36. "<iframe width=\"560\" height=\"315\" src=\"$1\" allowfullscreen></iframe>"
  37. );
  38. $HTML = preg_replace($a, $b, $HTML);
  39. $HTML = str_replace("http://www.youtube.com/watch?v=", "http://www.youtube.com/embed/", $HTML);
  40. $HTML = str_replace("&amp;list=UUWDzmLpJP-z4qopWVA4qfTQ", "", $HTML);
  41. $HTML = str_replace("&amp;index=1", "", $HTML);
  42. $HTML = str_replace("&amp;index=2", "", $HTML);
  43. $HTML = str_replace("&amp;index=3", "", $HTML);
  44. $HTML = str_replace("&amp;index=4", "", $HTML);
  45. $HTML = str_replace("&amp;index=5", "", $HTML);
  46. $HTML = str_replace("&amp;index=6", "", $HTML);
  47. $HTML = str_replace("&amp;index=7", "", $HTML);
  48. $HTML = str_replace("&amp;index=8", "", $HTML);
  49. $HTML = str_replace("&amp;index=9", "", $HTML);
  50. $HTML = str_replace("&amp;feature=plcp", "", $HTML);
  51. $HTML = str_replace("&amp;feature=related", "", $HTML);
  52. $HTML = str_replace("&amp;feature=player_embedded", "", $HTML);
  53. $HTML = str_replace("&amp;feature=fvwrel", "", $HTML);
  54. return $HTML;
  55. }
  56. /**
  57. * String Helper
  58. *
  59. * Cleans HTML from a String
  60. *
  61. * @param string $HTML
  62. * @return string $text
  63. */
  64. function cleanHTML($HTML) {
  65. $search = array(
  66. '@<script[^>]*?>.*?</script>@si',
  67. '@<[\/\!]*?[^<>]*?>@si',
  68. '@([\r\n])[\s]+@',
  69. '@&(quot|#34);@i',
  70. '@&(amp|#38);@i',
  71. '@&(lt|#60);@i',
  72. '@&(gt|#62);@i',
  73. '@&(nbsp|#160);@i',
  74. '@&(iexcl|#161);@i',
  75. '@&(cent|#162);@i',
  76. '@&(pound|#163);@i',
  77. '@&(copy|#169);@i',
  78. '@&#(\d+);@e'
  79. );
  80. $replace = array(
  81. '',
  82. '',
  83. '\1',
  84. '"',
  85. '&',
  86. '<',
  87. '>',
  88. ' ',
  89. chr(161),
  90. chr(162),
  91. chr(163),
  92. chr(169),
  93. 'chr(\1)'
  94. );
  95. return preg_replace($search, $replace, $HTML);
  96. }
  97. /**
  98. * compress
  99. *
  100. * Compresses a string
  101. *
  102. * @param string $text
  103. * @return string $text
  104. */
  105. function compress($string) {
  106. $string = str_replace(array("\r\n", "\r", "\n", "\t", " ", " ", " "), "", $string);
  107. return $string;
  108. }
  109. /**
  110. * cut
  111. *
  112. * Trims a string
  113. *
  114. * @param string $type = "Word"
  115. * @param string $text
  116. * @param string $length
  117. * @param string $nice
  118. * @param bool $file
  119. * @param bool $elipsis
  120. * @return string $
  121. */
  122. function cut($text, $length = 12, $type = "text", $slug = FALSE, $file = FALSE, $elipsis = FALSE) {
  123. if($type === "text") {
  124. $elipsis = "...";
  125. $words = explode(" ", $text);
  126. if(count($words) > $length) {
  127. return str_replace("\n", "", implode(" ", array_slice($words, 0, $length)) . $elipsis);
  128. }
  129. return $text;
  130. } elseif($type === "word") {
  131. if($file) {
  132. if(strlen($text) < $length) {
  133. $max = strlen($text);
  134. }
  135. if($slug) {
  136. return substr(slug($text), 0, $length);
  137. } else {
  138. return substr($text, 0, $length);
  139. }
  140. } else {
  141. if(strlen($text) < 13) {
  142. return $text;
  143. }
  144. if(!$elipsis) {
  145. if($slug) {
  146. return substr(slug($text), 0, $length);
  147. } else {
  148. return substr($text, 0, $length);
  149. }
  150. } else {
  151. if($slug) {
  152. return substr(slug($text), 0, $length) . $elipsis;
  153. } else {
  154. return substr($text, 0, $length) . $elipsis;
  155. }
  156. }
  157. }
  158. }
  159. }
  160. function exploding($string, $URL = NULL, $separator = ",") {
  161. if(strlen($string) > 0) {
  162. $string = str_replace(", ", ",", $string);
  163. $parts = explode($separator, $string);
  164. $count = count($parts) - 1;
  165. $return = NULL;
  166. if($count > 0) {
  167. for($i = 0; $i <= $count; $i++) {
  168. if(!is_null($URL)) {
  169. if($i === $count) {
  170. $return .= '<a href="'. path($URL . slug($parts[$i])) .'" title="'. $parts[$i] .'">'. $parts[$i] .'</a>';
  171. } elseif($i === $count - 1) {
  172. $return .= '<a href="'. path($URL . slug($parts[$i])) .'" title="'. $parts[$i] .'">'. $parts[$i] .'</a> '. __("and") .' ';
  173. } else {
  174. $return .= '<a href="'. path($URL . slug($parts[$i])) .'" title="'. $parts[$i] .'">'. $parts[$i] .'</a>, ';
  175. }
  176. } else {
  177. if($i === $count) {
  178. $return .= $parts[$i];
  179. } elseif($i === $count - 1) {
  180. $return .= $parts[$i] .' '. __("and") .' ';
  181. } else {
  182. $return .= $parts[$i] .', ';
  183. }
  184. }
  185. }
  186. return $return;
  187. } else {
  188. return '<a href="'. path($URL . slug($string)) .'" title="'. $string .'">'. $string .'</a>';
  189. }
  190. }
  191. return FALSE;
  192. }
  193. function like($ID = 0, $application = NULL, $likes = FALSE) {
  194. $likes = ($likes) ? " ($likes)" : NULL;
  195. if($ID > 0 and !is_null($application)) {
  196. return '<a title="'. __("I Like") .'" href="'. path("$application/like/$ID") .'"><img src="'. path("www/lib/images/like.png", TRUE) .'" /> '. __("I Like") . $likes .'</a>';
  197. }
  198. return FALSE;
  199. }
  200. function dislike($ID = 0, $application = NULL, $dislikes = FALSE) {
  201. $dislikes = ($dislikes) ? " ($dislikes)" : NULL;
  202. if($ID > 0 and !is_null($application)) {
  203. return '<a title="'. __("I Dislike") .'" href="'. path("$application/dislike/$ID") .'"><img src="'. path("www/lib/images/dislike.png", TRUE) .'" /> '. __("I Dislike") . $dislikes .'</a>';
  204. }
  205. return FALSE;
  206. }
  207. function report($ID = 0, $application = NULL) {
  208. if($ID > 0 and !is_null($application)) {
  209. return '<a title="'. __("Report Link") .'" href="'. path("$application/report/$ID") .'"><img src="'. path("www/lib/images/report.png", TRUE) .'" /> '. __("Report link") .'</a>';
  210. }
  211. return FALSE;
  212. }
  213. function decode($text, $URL = FALSE) {
  214. if(is_string($text)) {
  215. return (!$URL) ? utf8_decode($text) : urldecode($text);
  216. }
  217. return $text;
  218. }
  219. /**
  220. * encode
  221. *
  222. * Encodes a string and/or a URL
  223. *
  224. * @param string $text
  225. * @param string $URL = FALSE
  226. * @return string value
  227. */
  228. function encode($text, $URL = FALSE) {
  229. return (!$URL) ? utf8_encode($text) : urlencode($text);
  230. }
  231. /**
  232. * filter
  233. *
  234. * Cleans a string
  235. *
  236. * @param string $text
  237. * @param string $cleanHTML = FALSE
  238. * @return string $text
  239. */
  240. function filter($text, $filter = FALSE) {
  241. if(is_null($text)) {
  242. return FALSE;
  243. }
  244. if($text === TRUE) {
  245. return TRUE;
  246. } elseif($filter === TRUE) {
  247. $text = cleanHTML($text);
  248. } else {
  249. $text = addslashes($text);
  250. }
  251. $text = str_replace("<", "", $text);
  252. $text = str_replace(">", "", $text);
  253. $text = str_replace("%27", "", $text);
  254. $text = str_replace("%22", "", $text);
  255. $text = str_replace("%20", "", $text);
  256. $text = str_replace("indexphp", "index.php", $text);
  257. return $text;
  258. }
  259. function getBetween($content, $start, $end) {
  260. $array = explode($start, $content);
  261. if(isset($array[1])) {
  262. $array = explode($end, $array[1]);
  263. return $array[0];
  264. }
  265. return NULL;
  266. }
  267. function getTotal($count, $singular, $plural) {
  268. return ((int) $count === 0 or (int) $count > 1) ? (int) $count ." ". __(_($plural)) : (int) $count ." ". __(_($singular));
  269. }
  270. function gravatar($email) {
  271. return img("http://www.gravatar.com/avatar/". md5($email) ."");
  272. }
  273. function json($json, $encode = TRUE) {
  274. return ($encode) ? json_encode($json) : json_decode($json);
  275. }
  276. function parseCSV($file) {
  277. $fh = fopen($file, "r");
  278. while($line = fgetcsv($fh, 1000, ",")) {
  279. print $line[1];
  280. }
  281. }
  282. /**
  283. * pathToImages
  284. *
  285. * Add path to db images
  286. *
  287. * @author Daniel Chaur (@hasdman)
  288. * @param string $HTML
  289. * @param string $imagePath
  290. * @return string $HTML
  291. */
  292. function pathToImages($HTML = NULL, $imagePath = NULL) {
  293. if($HTML and $imagePath) {
  294. $newPath = ($imagePath === "lib") ? path("www/lib/images/", TRUE) : path("www/lib/themes/$imagePath/", TRUE);
  295. $patterns = array(
  296. '<img.*src="([^http].*?)".*?>',
  297. '/<a(.*)href="([^http].*\.(jpg|gif|png))"(.*)>(.*?)<\/a>/',
  298. '/url\(\'?([^\'\)]+)\'?\)/m'
  299. );
  300. $replacements = array(
  301. 'img src="'. $newPath .'\1" ',
  302. '<a$1href="'. $newPath .'$2"$4>$5</a> ',
  303. 'url('. $newPath .'\1) '
  304. );
  305. $HTML = preg_replace($patterns, $replacements, $HTML);
  306. return $HTML;
  307. }
  308. return FALSE;
  309. }
  310. function getCode($code) {
  311. if(!is_array($code)) {
  312. $code = explode("\n", $code);
  313. }
  314. $result = NULL;
  315. foreach($code as $line => $codeLine) {
  316. if(preg_match("/<\?(php)?[^[:graph:]]/", $codeLine)) {
  317. $result .= highlight_string($codeLine, TRUE) ."<br />";
  318. } else {
  319. $result .= preg_replace("/(&lt;\?php&nbsp;)+/", "", highlight_string("<?php ". $codeLine, TRUE)) ."<br />";
  320. }
  321. }
  322. return '<div class="code">'. $result .'</div>';
  323. }
  324. function showContent($content) {
  325. $content = str_replace("------", "", $content);
  326. $content = str_replace("\\", "", $content);
  327. return setCode($content, TRUE);
  328. }
  329. function setCode($HTML, $return = FALSE) {
  330. $codes = explode("[Code]", $HTML);
  331. if(count($codes) > 1) {
  332. for($i = 1; $i <= count($codes) - 1; $i++) {
  333. if(isset($codes[$i])) {
  334. $code = explode("[/Code]", $codes[$i]);
  335. if(isset($code[0])) {
  336. if($return) {
  337. $code[0] = getCode($code[0]);
  338. } else {
  339. $code[0] = addslashes($code[0]);
  340. }
  341. }
  342. if($return) {
  343. $codes[$i] = implode("", $code);
  344. } else {
  345. $codes[$i] = implode("[/Code]", $code);
  346. }
  347. }
  348. }
  349. }
  350. return ($return) ? implode("", $codes) : implode("[Code]", $codes);
  351. }
  352. function randomString($length = 6) {
  353. $consonant = array("b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "r", "s", "t", "v", "w", "x", "y", "z");
  354. $vocal = array("a", "e", "i", "o", "u");
  355. $string = NULL;
  356. srand((double) microtime() * 1000000);
  357. $max = $length / 2;
  358. for($i = 1; $i <= $max; $i++) {
  359. $string .= $consonant[rand(0, 19)];
  360. $string .= $vocal[rand(0, 4)];
  361. }
  362. return $string;
  363. }
  364. function repeat($string, $times = 2) {
  365. $HTML = NULL;
  366. for($i = 0; $i <= $times; $i++) {
  367. $HTML .= $string;
  368. }
  369. return $HTML;
  370. }
  371. /**
  372. * nice
  373. *
  374. * Gets the nice form of a String
  375. *
  376. * @param string $title
  377. * @return string $title
  378. */
  379. function slug($string) {
  380. $characters = array(
  381. "Á" => "A", "Ç" => "c", "É" => "e", "Í" => "i", "Ñ" => "n", "Ó" => "o", "Ú" => "u",
  382. "á" => "a", "ç" => "c", "é" => "e", "í" => "i", "ñ" => "n", "ó" => "o", "ú" => "u",
  383. "à" => "a", "è" => "e", "ì" => "i", "ò" => "o", "ù" => "u", "ã" => "a", "¿" => "",
  384. "?" => "", "¡" => "", "!" => "", ": " => "-"
  385. );
  386. $string = strtr($string, $characters);
  387. $string = strtolower(trim($string));
  388. $string = preg_replace("/[^a-z0-9-]/", "-", $string);
  389. $string = preg_replace("/-+/", "-", $string);
  390. if(substr($string, strlen($string) - 1, strlen($string)) === "-") {
  391. $string = substr($string, 0, strlen($string) - 1);
  392. }
  393. return $string;
  394. }
  395. function pageBreak($content, $URL = NULL) {
  396. $content = str_replace("<p><!-- pagebreak --></p>", "<!---->", $content);
  397. $content = str_replace('<p style="text-align: center;"><!-- pagebreak --></p>', "<!---->", $content);
  398. $content = str_replace('<p style="text-align: left;"><!-- pagebreak --></p>', "<!---->", $content);
  399. $content = str_replace('<p style="text-align: right;"><!-- pagebreak --></p>', "<!---->", $content);
  400. $content = str_replace('<p style="text-align: justify;"><!-- pagebreak --></p>', "<!---->", $content);
  401. $content = str_replace('<p style="text-align: center;"><span style="color: #ff0000;"><!----></span></p>', "<!---->", $content);
  402. $content = str_replace('<p style="text-align: center;"><em><!-- pagebreak --></em></p>', "<!---->", $content);
  403. $content = str_replace('<p style="text-align: center;"><strong><!-- pagebreak --></strong></p>', "<!---->", $content);
  404. $content = str_replace('<p style="text-align: center;"><span style="text-decoration: underline;"><!-- pagebreak --></span></p>', "<!---->", $content);
  405. $content = str_replace('<p style="text-align: justify;"><!-- pagebreak --></p>', "<!---->", $content);
  406. $content = str_replace('<p><!-- pagebreak -->', "<p><!-- pagebreak --></p>\n<p>", $content);
  407. $content = str_replace("<p><!-- pagebreak --></p>", "<!---->", $content);
  408. $content = str_replace('<!-- pagebreak -->', "<!---->", $content);
  409. $content = str_replace('<!-- Pagebreak -->', "<!---->", $content);
  410. $content = str_replace('<!--Pagebreak-->', "<!---->", $content);
  411. $content = str_replace('------', "<!---->", $content);
  412. $parts = explode("<!---->", $content);
  413. if(count($parts) > 1) {
  414. return $parts[0] .'<p><a href="'. $URL .'" title="'. __("Read more") .'">&raquo; '. __("Read more") .'...</a></p>';
  415. }
  416. return $content;
  417. }
  418. /**
  419. * POST
  420. *
  421. * Gets a specific position value from $_POST
  422. *
  423. * @param string $position
  424. * @param string $coding = "decode"
  425. * @return mixed
  426. */
  427. function POST($position = FALSE, $coding = "decode", $filter = "escape") {
  428. if($coding === "clean") {
  429. return $_POST[$position];
  430. } elseif($position === TRUE) {
  431. return $_POST;
  432. } elseif(!$position) {
  433. ____($_POST);
  434. } elseif(isset($_POST[$position]) and is_array($_POST[$position])) {
  435. $POST = $_POST[$position];
  436. } elseif(isset($_POST[$position]) and $_POST[$position] === "") {
  437. return NULL;
  438. } elseif(isset($_POST[$position])) {
  439. if($coding === "b64") {
  440. $POST = base64_decode($_POST[$position]);
  441. } elseif($coding === "unserialize") {
  442. $POST = unserialize(base64_decode($_POST[$position]));
  443. } elseif($coding === "encrypt") {
  444. if($filter === TRUE) {
  445. $POST = encrypt(encode($_POST[$position]));
  446. } elseif($filter === "escape") {
  447. $POST = encrypt(filter(encode($_POST[$position]), "escape"));
  448. } else {
  449. $POST = encrypt(filter(encode($_POST[$position]), TRUE));
  450. }
  451. } elseif($coding === "encode") {
  452. if($filter === TRUE) {
  453. $POST = encode($_POST[$position]);
  454. } elseif($filter === "escape") {
  455. $POST = filter(encode($_POST[$position]), "escape");
  456. } else {
  457. $POST = filter(encode($_POST[$position]), TRUE);
  458. }
  459. } elseif($coding === "decode-encrypt") {
  460. if($filter === TRUE) {
  461. $POST = encrypt(filter($_POST[$position], TRUE));
  462. } elseif($filter === "escape") {
  463. $POST = encrypt(filter($_POST[$position], "escape"));
  464. } else {
  465. $POST = encrypt($_POST[$position]);
  466. }
  467. } elseif($coding === "decode") {
  468. if($filter === TRUE) {
  469. $POST = filter(decode($_POST[$position]), TRUE);
  470. } elseif($filter === "escape") {
  471. $POST = filter(decode($_POST[$position]), "escape");
  472. } elseif($filter === NULL) {
  473. $POST = decode($_POST[$position]);
  474. } else {
  475. $data = decode($_POST[$position]);
  476. $data = str_replace("'", "\'", $data);
  477. $POST = $data;
  478. }
  479. } else {
  480. if($filter === TRUE) {
  481. $POST = filter($_POST[$position], TRUE);
  482. } elseif($filter === "escape") {
  483. $POST = filter($_POST[$position], "escape");
  484. } else {
  485. $POST = $_POST[$position];
  486. }
  487. }
  488. } else {
  489. return FALSE;
  490. }
  491. return $POST;
  492. }
  493. /**
  494. * recoverPOST
  495. *
  496. * Recovers data from $_POST
  497. *
  498. * @parama string $position
  499. * @parama string $value = NULL
  500. * @return string
  501. */
  502. function recoverPOST($position, $value = NULL) {
  503. if(!$value) {
  504. return (is_array(POST($position))) ? POST($position) : (POST($position) ? htmlentities(POST($position, "decode", FALSE)) : NULL);
  505. } else {
  506. if(is_array($value)) {
  507. foreach($value as $val) {
  508. $data[] = htmlentities($val);
  509. }
  510. return $data;
  511. } else {
  512. return (POST($position)) ? htmlentities(POST($position, "decode", FALSE)) : htmlentities(decode($value));
  513. }
  514. }
  515. }
  516. /**
  517. * removeSpaces
  518. *
  519. * Removes blank spaces
  520. *
  521. * @param string $text
  522. * @param bool $trim
  523. * @return string $text
  524. */
  525. function removeSpaces($text, $trim = FALSE) {
  526. $text = preg_replace("/\s+/", " ", $text);
  527. if($trim) {
  528. return trim($text);
  529. }
  530. return $text;
  531. }