0) { $r .= "\n"; } return $r; } public static function removeEmptyLines($string) { return preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", str_replace('\r\n', "\r\n", $string)); } public static function escape($t) { return htmlspecialchars(strip_tags(trim(str_replace(array("'", '"'), array("´", '´'), $t))), ENT_QUOTES); } public static function getPart($delimiter, $string, $index = null) { $parts = explode($delimiter, $string); if (is_numeric($index)) { return isSet($parts[$index]) ? $parts[$index] : false; }else{ return $parts; } } public static function getMonth($id, $lang) { if ($lang == 'cs') { switch($id) { case 1: return 'leden'; break; case 2: return 'únor'; break; case 3: return 'březen'; break; case 4: return 'duben'; break; case 5: return 'květen'; break; case 6: return 'červen'; break; case 7: return 'červenec'; break; case 8: return 'srpen'; break; case 9: return 'září'; break; case 10: return 'říjen'; break; case 11: return 'listopad'; break; case 12: return 'prosinec'; break; } } } public static function getMonthShort($id, $lang) { if ($lang == 'cs') { switch($id) { case 1: return 'led'; break; case 2: return 'úno'; break; case 3: return 'bře'; break; case 4: return 'dub'; break; case 5: return 'kvě'; break; case 6: return 'črn'; break; case 7: return 'črc'; break; case 8: return 'srp'; break; case 9: return 'zář'; break; case 10: return 'říj'; break; case 11: return 'lis'; break; case 12: return 'pro'; break; } } } public static function getParts($delimiter, $string, $maxIndex = 1) { $parts = Class_String::getPart($delimiter, $string); $text = ''; foreach((array)$parts as $key=>$item) { $text .= $delimiter . $item; if ($key >= $maxIndex) { break; } } return $text; } public function remSpecChars($string) { $from = explode(" ",", ; ! @ # $ % ^ & * ( ) : < > ? ~ ' + { } [ ] / \" \\"); $to = explode(" ","- - - - - - - - - - - - - - - - - - - - - - - - - -"); return str_replace($from,$to,$string); } public function makeIdent($string) { $string = Class_String::remDiac($string); $string = Class_String::remSpecChars($string); $string = iconv('utf-8', 'ascii//translit', mb_strtolower($string)); $string = preg_replace(",[^\w_/-],","-",$string); $string = preg_replace("/-{2,}/","-",$string); $string = preg_replace("#/{2,}#","/",$string); $string = preg_replace("/^-|-$/","",$string); return $string; } public function formatFileSize($size, $mask = "%s %s") { $index = 0; $units = array("B","KB","MB","GB","TB","PT"); while($size > 1024) { $size = $size / 1024; $index++; } return sprintf($mask,str_replace(' ',' ',number_format($size,2,',',' ')),$units[$index]); } public function formatPhone($string) { $phones = preg_split("/\s*,\s*/",$string); foreach($phones as $key => &$value) { $value = str_replace(" "," ",$value); } return join(", ",$phones); } public static function generatePassword($length = 12) { $array = array(); for($i=0;$i<26;$i++) { $array[] = 65+$i; $array[] = 97+$i; } for($i=0;$i<=9;$i++) { $array[] = 48+$i; } for($i=0;$i<$length;$i++) { $passwd .= chr($array[rand(0, count($array)-1)]); } return $passwd; } public static function myStrStr($haystack, $needle, $before_needle = false) { //Find position of $needle or abort if(($pos=strpos($haystack,$needle))===FALSE) return FALSE; if($before_needle) return trim(substr($haystack,0,$pos+strlen($needle)), '/'); else return trim(substr($haystack,$pos), '/'); } public static function formatNumber($value, $decimals = 2, $separator = ' ', $point = '.') { $value = number_format($value, $decimals, $point, $separator); if($separator == ' ') { $number = str_replace($separator, utf8_encode("\xA0"), $value); } return $number; } public static function smartTrim($string, $max_length, $end_substitute = '...', $html_linebreaks = true) { if($html_linebreaks) $string = preg_replace('/\/i', "\n", $string); $string = strip_tags($string); //gets rid of the HTML if(empty($string) || mb_strlen($string) <= $max_length) { if($html_linebreaks) $string = nl2br($string); return $string; } if($end_substitute) $max_length -= mb_strlen($end_substitute, 'UTF-8'); $stack_count = 0; while($max_length > 0){ $char = mb_substr($string, --$max_length, 1, 'UTF-8'); if(preg_match('#[^\p{L}\p{N}]#iu', $char)) $stack_count++; //only alnum characters elseif($stack_count > 0) { $max_length++; break; } } $string = mb_substr($string, 0, $max_length, 'UTF-8').$end_substitute; if($html_linebreaks) $string = nl2br($string); return $string; } /** * Trim the string. * * @param string $text The line to trim. * @param integer $max_len The maximum length of the trimmed line. * This ignores the length of the characters * that indicate trimming has occured. * @param boolean $trim_middle Trimming takes place in the middle of the line * iff true. Otherwise, the line is trimmed at the * end. Defaults to false. * @param string $trim_chars Characters to use to indicate trimming has * occured. Defaults to '...'. * * @return string The trimmed line of text. */ public static function smartTrim2($text, $max_len, $trim_middle = false, $trim_chars = '...') { $text = trim($text); if (mb_strlen($text) < $max_len) { return $text; }elseif($trim_middle){ $hasSpace = mb_strpos($text, ' '); if (!$hasSpace) { /** * The entire string is one word. Just take a piece of the * beginning and a piece of the end. */ $first_half = mb_substr($text, 0, $max_len / 2); $last_half = mb_substr($text, - ($max_len - mb_strlen($first_half))); }else{ /** * Get last half first as it makes it more likely for the first * half to be of greater length. This is done because usually the * first half of a string is more recognizable. The last half can * be at most half of the maximum length and is potentially * shorter (only the last word). */ $last_half = mb_substr($text, -($max_len / 2)); $last_half = trim($last_half); $last_space = mb_strrpos($last_half, ' '); if (!($last_space === false)) { $last_half = mb_substr($last_half, $last_space + 1); } $first_half = mb_substr($text, 0, $max_len - mb_strlen($last_half)); $first_half = trim($first_half); if (mb_substr($text, $max_len - mb_strlen($last_half), 1) == ' ') { /** * The first half of the string was chopped at a space. */ $first_space = $max_len - mb_strlen($last_half); }else{ $first_space = mb_strrpos($first_half, ' '); } if (!($first_space === false)) { $first_half = mb_substr($text, 0, $first_space); } } return $first_half.$trim_chars.$last_half; }else{ $trimmed_text = mb_substr($text, 0, $max_len); $trimmed_text = trim($trimmed_text); if (mb_substr($text, $max_len, 1) == ' ') { /** * The string was chopped at a space. */ $last_space = $max_len; }else{ /** * In PHP5, we can use 'offset' here -Mike */ $last_space = mb_strrpos($trimmed_text, ' '); } if (!($last_space === false)) { $trimmed_text = mb_substr($trimmed_text, 0, $last_space); } return Class_String::removeTrailingPunctuation($trimmed_text).$trim_chars; } } public static function reduceString($text, $part1 = 40, $part2 = 10) { $text = trim($text); if (strlen($text) > $part1) { $text_1 = mb_substr($text, 0, $part1); if ($part2 > 0) { if (strlen($text) > ($part1 + $part2)) { $text_2 = mb_substr($text, $part2 * (-1)); } $text = $text_1 . '...' . $text_2; }else{ $text = $text_1 . ' ...'; } } return $text; } /** * Strip trailing punctuation from a line of text. * * @param string $text The text to have trailing punctuation removed from. * * @return string The line of text with trailing punctuation removed. */ public static function removeTrailingPunctuation($text) { return preg_replace("'[^a-zA-Z_0-9]+$'s", '', $text); } /** * Universal date/time parser * * @param mixed $value The date and/or time we need parse * @return int Unix time-stamp */ function parse_date_time($value, $adodb = true) { if($adodb){ $adodb = function_exists('adodb_mktime'); } if (! $value) { return 0; } // zero datetime if (strspn($value, '0-: ') == strlen($value) && strlen($value) > 9) { return 0; } // if(!$adodb){ $ts = strtotime($value); if ($ts != -1 && $ts !== false) { return $ts; } // } $value = trim($value); // d.m[.Y] if (preg_match('/^(\d+)\.\s*(\d+)\.?\s*(\d+)?$/', $value, $m)) { if($adodb) { return adodb_mktime(0, 0, 0, $m[2], $m[1], isset($m[3]) ? $m[3] : adodb_date('Y')); } else { return mktime(0, 0, 0, $m[2], $m[1], isset($m[3]) ? $m[3] : date('Y')); } } // d.m[.Y] H:i[:s] if (preg_match(',(\d+)\.\s*(\d+)\.\s*(\d+)?\.?\s+(\d+)[.:]\s*(\d+)[.:]?\s*(\d+)?,',$value, $m)) { if($adodb) { return adodb_mktime( $m[4], $m[5], empty($m[6]) ? 0 : $m[6], $m[2], $m[1], $m[3] ? $m[3] : adodb_date('Y') ); } else { return mktime( $m[4], $m[5], empty($m[6]) ? 0 : $m[6], $m[2], $m[1], $m[3] ? $m[3] : date('Y') ); } } // [Y-]m-d H:i[:s] if (preg_match(',(\d+)?\-?(\d+)\-(\d+)\s+(\d+)[.:]\s*(\d+)[.:]?\s*(\d+)?,',$value, $m)) { if($adodb){ return adodb_mktime( $m[4], $m[5], empty($m[6]) ? 0 : $m[6], $m[2], $m[3], $m[1] ? $m[1] : adodb_date('Y') ); } else { return mktime( $m[4], $m[5], empty($m[6]) ? 0 : $m[6], $m[2], $m[3], $m[1] ? $m[1] : date('Y') ); } } // [Y-]m-d if (preg_match(',(\d+)?\-?(\d+)\-(\d+),',$value, $m)) { if($adodb){ return adodb_mktime(0, 0, 0, $m[2], $m[3], $m[1] ? $m[1] : adodb_date('Y') ); } else { return mktime(0, 0, 0, $m[2], $m[3], $m[1] ? $m[1] : date('Y') ); } } return -1; } public static function makeRelativeUrl($ident) { if ($ident[0] != '/') { $ident = '/' . $ident; } if (strlen($ident) > 1 && substr($ident, -1) != '/') { $ident .= '/'; } return $ident; } public static function is($text) { if (trim(strip_tags($text)) == '') { return false; } return true; } } ?>