X-Git-Url: https://jfr.im/git/irc/unrealircd/unrealircd-webpanel.git/blobdiff_plain/fe2a6f275bbe952f2eed1bd7314ebae7c2e8bc72..dfa0ec8e77c09871f817793f40148da19672f786:/misc/strings.php diff --git a/misc/strings.php b/misc/strings.php index 9678bda..1fecc53 100644 --- a/misc/strings.php +++ b/misc/strings.php @@ -14,6 +14,12 @@ function split($str, $delimiter = " ") : Array return explode($delimiter,$str); } +/** + * + * @param mixed $array + * @param mixed $delimiter + * @return string + */ function glue($array, $delimiter = " ") { $string = ""; @@ -26,6 +32,11 @@ function glue($array, $delimiter = " ") return trim($string,$delimiter); } +/** + * Gets the relative path of the filename + * @param mixed $filename + * @return string + */ function get_relative_path($filename) { $relativepath = split($filename, "/"); @@ -38,4 +49,142 @@ function get_relative_path($filename) } $relativepath = glue($relativepath,"/"); return $relativepath; +} + +/** + * Returns a `nick` if the string was in the syntax: + * nick!ident@host + * @param mixed $str + * @return mixed + */ +function show_nick_only($str) +{ + $x = strpos($str, "!"); + if ($x !== false) + $str = substr($str, 0, $x); + return $str; +} + +/** + * Figures out how long ago a given time was + * returns string example: + * - "32 minutes ago" + * - "5 hours ago" + * - "12 seconds ago" + */ +function how_long_ago($timestamp) +{ + $now = time(); + $diff = $now - strtotime($timestamp); + $units = array( + 31536000 => 'year', + 2592000 => 'month', + 604800 => 'week', + 86400 => 'day', + 3600 => 'hour', + 60 => 'minute', + 1 => 'second' + ); + + foreach ($units as $unit => $text) { + if ($diff < $unit) continue; + $numberOfUnits = floor($diff / $unit); + return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'').' ago'; + } +} + +/** + * Uses system time. + * Returns: + * - evening + * - morning + * - afternoon + */ +function time_of_day() +{ + $timeofday = "day"; // in case something went wrong? lol + $hour = date("H"); + if ($hour >= 18 || $hour < 4) + $timeofday = "evening"; + else if ($hour >= 4 && $hour < 12) + $timeofday = "morning"; + else if ($hour >= 12 && $hour < 18) + $timeofday = "afternoon"; + + return $timeofday; +} + + +/** + * Concatenate a string to a string + */ +function strcat(&$targ,$string) : void +{ $targ .= $string; } + + +/** + * Concatenate a string to a string and limits the string to a certain length + */ +function strlcat(&$targ,$string,$size) : void +{ + strcat($targ,$string); + $targ = mb_substr($targ,0,$size); +} + + +/** + * Prefixes a string to a string + */ +function strprefix(&$targ,$string) : void +{ $targ = $string.$targ; } + + +/** + * Prefixes a string to a string and limits the string to a certain length + */ +function strlprefix(&$targ,$string,$size) : void +{ + if (sizeof($targ) >= $size) + return; + + strprefix($targ,$string); + $targ = mb_substr($targ,0,$size); +} + +/** Checks if the token provided is a bad pointer, by reference + * Returns Bool value true if it IS bad + * + * Syntax: + * BadPtr($variable) + * + * Returns: + * @ +*/ +function BadPtr(&$tok) +{ + if (!isset($tok) || empty($tok) || !$tok || strlen($tok) == 0) + return true; + return false; +} + +/** This function takes a string, tokenizes + * it by a space (chr 32), removes the first + * word/token, and returns the result. + * Mostly used for string manipulation around + * the source. + * + * Syntax: + * rparv(String $sentence) + * + * Returns: + * string|false + */ +function rparv($string) +{ + $parv = split($string); + $first = strlen($parv[0]) + 1; + $string = substr($string, $first); + if ($string) + return $string; + return false; } \ No newline at end of file