Advertisement
rodro1

Helper Php

Jan 7th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.48 KB | None | 0 0
  1. <?php
  2. class Helper {
  3.  
  4.    
  5.     public static function getActive($page = null) {
  6.         if(!empty($page)) {
  7.             if(is_array($page)) {
  8.                 $error = array();
  9.                 foreach($page as $key => $value) {
  10.                     if(Url::getParam($key) != $value) {
  11.                         array_push($error, $key);
  12.                     }
  13.                 }
  14.                 return empty($error) ? " class=\"act\"" : null;
  15.             }
  16.         }
  17.         return $page == Url::cPage() ? " class=\"act\"" : null;
  18.     }
  19.    
  20.     public static function encodeHTML($string, $case = 2) {
  21.         switch($case) {
  22.             case 1:
  23.             return htmlentities($string, ENT_NOQUOTES, 'UTF-8', false);
  24.             break;         
  25.             case 2:
  26.             $pattern = '<([a-zA-Z0-9\.\, "\'_\/\-\+~=;:\(\)?&#%![\]@]+)>';
  27.             // put text only, devided with html tags into array
  28.             $textMatches = preg_split('/' . $pattern . '/', $string);
  29.             // array for sanitised output
  30.             $textSanitised = array();          
  31.             foreach($textMatches as $key => $value) {
  32.                 $textSanitised[$key] = htmlentities(html_entity_decode($value, ENT_QUOTES, 'UTF-8'), ENT_QUOTES, 'UTF-8');
  33.             }          
  34.             foreach($textMatches as $key => $value) {
  35.                 $string = str_replace($value, $textSanitised[$key], $string);
  36.             }          
  37.             return $string;        
  38.             break;
  39.         }
  40.     }
  41.    
  42.    
  43.     public static function getImgSize($image, $case) {
  44.         if(is_file($image)) {
  45.             // 0 => width, 1 => height, 2 => type, 3 => attributes
  46.             $size = getimagesize($image);
  47.             return $size[$case];
  48.         }
  49.     }
  50.    
  51.    
  52.     public static function shortenString($string, $len = 150) {
  53.         if (strlen($string) > $len) {
  54.             $string = trim(substr($string, 0, $len));
  55.             $string = substr($string, 0, strrpos($string, " "))."&hellip;";
  56.         } else {
  57.             $string .= "&hellip;";
  58.         }
  59.         return $string;
  60.     }
  61.    
  62.    
  63.    
  64.    
  65.    
  66.    
  67.     public static function redirect($url = null) {
  68.         if (!empty($url)) {
  69.             header("Location: {$url}");
  70.             exit;
  71.         }
  72.     }
  73.    
  74.    
  75.    
  76.    
  77.    
  78.    
  79.    
  80.     public static function setDate($case = null, $date = null) {
  81.        
  82.         $date = empty($date) ? time() : strtotime($date);
  83.        
  84.         switch($case) {
  85.             case 1:
  86.             // 01/01/2010
  87.             return date('d/m/Y', $date);
  88.             break;
  89.             case 2:
  90.             // Monday, 1st January 2010, 09:30:56
  91.             return date('l, jS F Y, H:i:s', $date);
  92.             break;
  93.             case 3:
  94.             // 2010-01-01-09-30-56
  95.             return date('Y-m-d-H-i-s', $date);
  96.             break;
  97.             default:
  98.             return date('Y-m-d H:i:s', $date);
  99.         }
  100.    
  101.     }
  102.    
  103.    
  104.    
  105.    
  106.    
  107.    
  108.    
  109.    
  110.    
  111.     public static function cleanString($name = null) {
  112.         if (!empty($name)) {
  113.             return strtolower(preg_replace('/[^a-zA-Z0-9.]/', '-', $name));
  114.         }
  115.     }
  116.    
  117.    
  118.    
  119.    
  120.    
  121.    
  122.    
  123.    
  124.    
  125.    
  126.    
  127.    
  128.    
  129.    
  130.    
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement