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