]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blobdiff - misc/strings.php
Channel details: re-add the Host column in "User List", but only fill
[irc/unrealircd/unrealircd-webpanel.git] / misc / strings.php
index 9678bdaeb5021ba2a2a7af6c1866af8a1e7094a6..1fecc53c443c2fe96f9f3544faed116ee44cc5a9 100644 (file)
@@ -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