window.setTimeout(function(){location.href="'.$url.'"},'.$timeout.');'; } else { echo ''; } } /** * Return een random string * * @param int $length lengte van de gevraagde random string * @return string random string **/ public static function random($length=16,$onlyalpha=true){ if($onlyalpha) $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; else $chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; return substr(str_shuffle($chars), 0, $length); } public static function price($input,$output='nice'){ switch($output){ case "nl": //input = sql return number_format((double)$input,2,",",""); break; case "nice": return money_format('%.2n', (double)$input); break; case "sql": return Str::filter(str_replace(',','.',$input),'DECIMAL'); break; } } //todo dit rewriteN: /** mogelijke filters: * ALPHA = alleen letters * NUMERIC = alleen nummers * ALPHANUMERIC = alleen letters en nummers * ALPHANUMERICDASH = alleen letters en nummers en dash (-) */ public static function filter($input,$filter='ALPHANUMERIC',$forcecase=false){ $input = str_replace('@','a',$input); switch($filter){ case "PLAIN": return str_replace(array("\n","\r"),"",strip_tags($input)); break; case "ALPHA": $output = preg_replace("/[^A-Za-z]/",'',$input); break; case "NUMERIC": $output = preg_replace("/[^0-9]/",'',$input); break; case "DECIMAL": $output = preg_replace("/[^0-9.]/",'',$input); break; case "ALPHANUMERIC": $output = preg_replace("/[^A-Za-z0-9]/",'',$input); break; case "ALPHANUMERICDASH": $output = str_replace(" ","-",$input); $output = preg_replace("/[^A-Za-z0-9_-]/",'',$output); break; } if($forcecase){ if($forcecase=='lowercase') $output = strtolower($output); elseif($forcecase=='uppercase') $output = strtoupper($output); elseif($forcecase=='ucfirst') $output = ucfirst(strtolower($output)); } //evt dubbele dashes replacen met een $output = str_replace('--','-',$output); return $output; } public static function pad($input,$minlength=1){ if($minlength!=1){ return str_pad($input,$minlength,' '); } else { if($input=='') return ' '; else return $input; } } /** * Str::safe = htmlspecialchars * * @param string $var string om te parsen * @return string geparste string */ public static function safe($var){ return htmlspecialchars($var); } /** * Parse url; voegt http:// toe als dit nog niet in de string zit * * @param string $url url om te parsen * @return string geparste url */ public static function parseUrl($url){ if($url=='') return $url; if (preg_match("#https?://#", $url) === 0) { $url = 'http://'.$url; } return $url; } public static function date($input='',$outputformat='nl'){ if($input=='') $ts = time(); elseif (is_numeric($input)) $ts = $input; else $ts = strtotime($input); switch($outputformat){ case "nl": return strftime('%d-%m-%Y',$ts); break; case "nlwtime": return strftime('%d-%m-%Y %H:%M',$ts); break; case "nllargewtime": return ucfirst(strftime('%A %d %B %Y %H:%M',$ts)); break; case "nllarge": return ucfirst(strftime('%A %d %B %Y',$ts)); break; case "sql": return strftime('%Y-%m-%d',$ts); break; default: return strftime($outputformat,$ts); break; } } /** * Str::date; returnt date van een SQL/Engelse date(time) string naar gewenst format * * @param string $datetime datetime om te formatten * @return string $format outputformat. Kan zijn: nl/nllarge of een strtotime() format */ public static function time($time,$format='nl'){ if($time=='') $time='12:00'; $ts = strtotime($time); switch($format){ case "nl": default: $output = strftime('%H:%M',$ts); break; } return $output; } public static function sqlDateTime($ts=false){ if($ts==false) $ts = time(); return strftime('%Y-%m-%d %H:%M:%S',$ts); } /** * Str::parseTweet; format een twitter-tweet * * @param string $string string van de tweet * * @return string geformatte html tweet-content */ /*nog niet compleet */ public static function parseTweet($string){ /*** make sure there is an http:// on all URLs ***/ $string = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i", "$1http://$2",$string); /*** make all URLs links ***/ $string = preg_replace("/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i","$1",$string); $string = preg_replace('/(^|\s)@(\w+)/','\1@\2',$string); $string = preg_replace('/(^|\s)#(\w+)/','\1#\2',$string); return $string; } //alias voor htmlspecialchars public static function hc($str){ return htmlspecialchars($str); } /** soort substr maar dan alleen afkappen op hele woorden **/ public static function shorten($string,$length=160,$striptags = true,$adddots=false) { if ($striptags) $string = strip_tags($string); if (preg_match('/^.{1,'.$length.'}\b/su', $string, $match)) { $line=$match[0]; } if($adddots && $line!=$string) $line .= " ..."; return $line; } public static function parseTags($input,$tag='fig',$txt='Fig. $1'){ #$input = "[link=http://www.google.com]test[/link]"; #$replacement = preg_replace('/\[link=(.*?)\](.*?)\[\/link\]/', '$2', $input); return preg_replace('/\['.$tag.'=(.*?)\]/', ''.$txt.'', $input); } public static function parseSq($input,$sq=''){ $words = array_filter(explode(' ',$sq)); foreach($words as $word){ $word = trim($word); $input = preg_replace('/('.$word.')/i', '$1', $input); } return $input; } public static function urlEncodeJSON($data){ return rawurlencode(json_encode($data)); } public static function urlDecodeJSON($str){ return json_decode(rawurldecode($str),true); } } ?>