-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.php
More file actions
75 lines (61 loc) · 2.33 KB
/
utilities.php
File metadata and controls
75 lines (61 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
// -- include/functions/utilities.php
// ---------------------------------------------------------------------------------------------
/**
* void predump( mixed $var )
* Dumps the contents of a variable wrapped in <pre> tags for easier debugging.
*
* @param mixed $var The variable being dumped.
* ------------------------------------------------------------------------------------------ */
function predump( $var )
{
echo '<pre>'. print_r( $var, true ) .'</pre>'."\n";
}
/**
* void quickdump( mixed $var )
* Alias of predump(), included since that's what we're used to calling it.
*
* @deprecated 0.1.0
* ------------------------------------------------------------------------------------------ */
function quickdump( $var )
{
predump( $var );
}
/**
* mixed get_version_hash( string $filepath [, int $length = 7 [, string $alg = 'sha1']] )
* Generates a unique hash using hash_file() that can be used for file versioning/caching.
*
* @param string $filepath Absolute server path to the file.
* @param int $length Trim the hash to the specified length.
* @param string $alg The hash algorithm to use.
*
* @return string|bool The trimmed string, or FALSE if $filepath doesn't exist.
* ------------------------------------------------------------------------------------------ */
function get_version_hash( $filepath, $length = 7, $alg = 'sha1' )
{
if ( !file_exists( $filepath ) )
return false;
$hash = hash_file( $alg, $filepath );
if ( strlen( $hash ) > $length )
$hash = substr( $hash, 0, $length );
return $hash;
}
/**
* string minify( string $html )
* Strips extraneous whitespace from a chunk of HTML.
*
* @param string $html The HTML to minify.
*
* @return string The minified HTML.
* ------------------------------------------------------------------------------------------ */
function minify( $html )
{
// Remove whitespace between tags
$html = preg_replace( '/(?<=\>)[\r\n\t]+(?=\<)/', '', $html );
// Restrict whitespace between the beginning/end of a tag and text
$html = preg_replace( '/(?<=\>)[\r\n\t\s]+(?=[^\<])/', ' ', $html );
$html = preg_replace( '/(?<=[^\>])[\r\n\t\s]+(?=\<)/', ' ', $html );
// Remove linebreaks in blocks of text
$html = preg_replace( '/(?<=[^\-\<\>])[\r\n\t\s]+(?=[^\-\<\>])/', ' ', $html );
return $html;
}