diff --git a/class-OutputUtilities.php b/class-OutputUtilities.php index 3b759e2..96ba1fa 100644 --- a/class-OutputUtilities.php +++ b/class-OutputUtilities.php @@ -1,181 +1,183 @@ . /** * Collection of utilities */ class OutputUtilities { /** * Generate a slug * * @param string $s Input string e.g. 'what the hell' * @param int $max_length Max. length limit * @param string $glue Slug glue * @param bool $truncated Flag to indicate if the string is truncated by the $max_length limit * @return 'something-as-this' */ public static function slug( $s, $max_length = -1, $glue = '-', & $truncated ) { $truncated = false; $s = strtolower( self::stripAccents($s) ); if( $glue !== '_' ) { $s = str_replace( '_',' ', $s ); } $s = preg_replace( "/[^a-z0-9\s\\$glue]/", '', $s ); $s = preg_replace( "/[\s\\$glue]+/", ' ', $s ); $s = trim( $s, ' ' ); if( $max_length !== -1 ) { $len = strlen( $s ); $s = substr( $s, 0, $max_length ); $truncated = $len !== strlen( $s ); } $s = preg_replace( '/\s/', $glue, $s ); return rtrim( $s, $glue ); } /** * Enfatize a substring * * @param $s string Heystack * @param $q string Needle * @param $pre string HTML before query (bold tag as default) * @param $post string HTML after query (bold tag as default) * @return string Enfatized string * @todo Move in it's own class */ - public static function enfatizeSubstr( $s, $q, $pre = "", $post = "" ) { + public static function enfatizeSubstr( $s, $q, $pre = '', $post = '' ) { + + $out = ''; // no needle? that's quick if( empty( $q ) ) { return $s; } - $s_length = mb_strlen( $s ); $q_length = mb_strlen( $q ); $offset = 0; do { // find occurrence $pos = mb_stripos( $s, $q, $offset ); if( $pos === false ) { break; } // enfatize query $enfatized = $pre . mb_substr( $s, $pos, $q_length ) . $post; $enfatized_length = mb_strlen( $enfatized ); // pre-query and post-query strings $s_pre = mb_substr( $s, 0, $pos ); $s_post = mb_substr( $s, $pos + $q_length ); // save $s = $s_pre . $enfatized . $s_post; + $s_length = mb_strlen( $s ); $offset = $pos + $enfatized_length; } while( $offset < $s_length ); return $s; } /** * Get the human filesize from bytes * * @param $filesize int bytes * @param $glue string * @return string */ public static function humanFilesize( $filesize, $glue = ' ' ){ if( ! is_numeric( $filesize ) ) { return __( "NaN" ); } $decr = 1024; $step = 0; $prefix = [ 'B', 'KB', 'MB', 'GB', 'TB', 'PB' ]; while( $filesize / $decr > 0.9 ) { $filesize /= $decr; $step++; } return round( $filesize, 2 ) . $glue . $prefix[ $step ]; } /** * Strip accents. * * @param $s string * @return string */ public static function stripAccents( $s ) { $a = ['À','Á','Â','Ã','Ä','Å','Æ', 'Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö','Ø','Ù','Ú','Û','Ü','Ý','ß','à','á','â','ã','ä','å','æ', 'ç','è','é','ê','ë','ì','í','î','ï','ñ','ò','ó','ô','õ','ö','ø','ù','ú','û','ü','ý','ÿ','Ā','ā','Ă','ă','Ą','ą','Ć','ć','Ĉ','ĉ','Ċ','ċ','Č','č','Ď','ď','Đ','đ','Ē','ē','Ĕ','ĕ','Ė','ė','Ę','ę','Ě','ě','Ĝ','ĝ','Ğ','ğ','Ġ','ġ','Ģ','ģ','Ĥ','ĥ','Ħ','ħ','Ĩ','ĩ','Ī','ī','Ĭ','ĭ','Į','į','İ','ı','IJ','ij','Ĵ','ĵ','Ķ','ķ','Ĺ','ĺ','Ļ','ļ','Ľ','ľ','Ŀ','ŀ','Ł','ł','Ń','ń','Ņ','ņ','Ň','ň','ʼn','Ō','ō','Ŏ','ŏ','Ő','ő','Œ','œ','Ŕ','ŕ','Ŗ','ŗ','Ř','ř','Ś','ś','Ŝ','ŝ','Ş','ş','Š','š','Ţ','ţ','Ť','ť','Ŧ','ŧ','Ũ','ũ','Ū','ū','Ŭ','ŭ','Ů','ů','Ű','ű','Ų','ų','Ŵ','ŵ','Ŷ','ŷ','Ÿ','Ź','ź','Ż','ż','Ž','ž','ſ','ƒ','Ơ','ơ','Ư','ư','Ǎ','ǎ','Ǐ','ǐ','Ǒ','ǒ','Ǔ','ǔ','Ǖ','ǖ','Ǘ','ǘ','Ǚ','ǚ','Ǜ','ǜ','Ǻ','ǻ','Ǽ','ǽ','Ǿ','ǿ','Ά','ά','Έ','έ','Ό','ό','Ώ','ώ','Ί','ί','ϊ','ΐ','Ύ','ύ','ϋ','ΰ','Ή','ή']; $b = ['A','A','A','A','A','A','AE','C','E','E','E','E','I','I','I','I','D','N','O','O','O','O','O','O','U','U','U','U','Y','s','a','a','a','a','a','a','ae','c','e','e','e','e','i','i','i','i','n','o','o','o','o','o','o','u','u','u','u','y','y','A','a','A','a','A','a','C','c','C','c','C','c','C','c','D','d','D','d','E','e','E','e','E','e','E','e','E','e','G','g','G','g','G','g','G','g','H','h','H','h','I','i','I','i','I','i','I','i','I','i','IJ','ij','J','j','K','k','L','l','L','l','L','l','L','l','l','l','N','n','N','n','N','n','n','O','o','O','o','O','o','OE','oe','R','r','R','r','R','r','S','s','S','s','S','s','S','s','T','t','T','t','T','t','U','u','U','u','U','u','U','u','U','u','U','u','W','w','Y','y','Y','Z','z','Z','z','Z','z','s','f','O','o','U','u','A','a','I','i','O','o','U','u','U','u','U','u','U','u','U','u','A','a','AE','ae','O','o','Α','α','Ε','ε','Ο','ο','Ω','ω','Ι','ι','ι','ι','Υ','υ','υ','υ','Η','η']; return str_replace( $a, $b, $s ); } /** * Spawn the white screen of death * * @param $msg string */ public static function WSOD( $msg ) { self::header503(); ?> <?php _e("Errore") ?>

\n"; echo '

' . esc_html( $msg ) . '

'; } } /** * Send HTTP 503 headers */ public static function header503() { if( headers_sent() ) { error( 'cannot send headers if there is earlier output' ); } else { header( 'HTTP/1.1 503 Service Temporarily Unavailable' ); header( 'Status: 503 Service Temporarily Unavailable' ); header( 'Retry-After: 300' ); } } }