diff options
author | steckbrief <steckbrief@chefmail.de> | 2017-11-25 23:30:49 +0100 |
---|---|---|
committer | steckbrief <steckbrief@chefmail.de> | 2017-11-25 23:30:49 +0100 |
commit | 03cf2a22fe227633cf901dc1d9cf1024b8d07b59 (patch) | |
tree | 7a5a4e2162e088bf745161cb65d0a852e3e51010 /functions.common.inc.php |
Initial commit
Diffstat (limited to 'functions.common.inc.php')
-rw-r--r-- | functions.common.inc.php | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/functions.common.inc.php b/functions.common.inc.php new file mode 100644 index 0000000..5be37b6 --- /dev/null +++ b/functions.common.inc.php @@ -0,0 +1,40 @@ +<?php +/* + * This file contains functions commonly used. + */ + +/** + * Copied from http://rogerstringer.com/2013/11/15/generate-uuids-php/ + */ +function generate_uuid() { + return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', + mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), + mt_rand( 0, 0xffff ), + mt_rand( 0, 0x0fff ) | 0x4000, + mt_rand( 0, 0x3fff ) | 0x8000, + mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) + ); +} + +function format_size($size, $precision = 2) { + $sizes = ['bytes', 'Kb', 'Mb', 'Gb', 'Tb']; + $i = 0; + while (1023 < $size && $i < count($sizes) - 1) { + $size /= 1023; + ++$i; + } + + return number_format($size, $precision).' '.$sizes[$i]; +} + +function startsWith($haystack, $needle) { + $length = strlen($needle); + return (substr($haystack, 0, $length) === $needle); +} + +function endsWith($haystack, $needle) { + $length = strlen($needle); + + return $length === 0 || (substr($haystack, -$length) === $needle); +} +?> |