]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blobdiff - misc/strings.php
Fix setting zlines on idents, reported by Jellis
[irc/unrealircd/unrealircd-webpanel.git] / misc / strings.php
index 9678bdaeb5021ba2a2a7af6c1866af8a1e7094a6..f5d576753ee66e197be5aac0deb49d41afa7f679 100644 (file)
@@ -1,5 +1,10 @@
 <?php
 
+function to_slug($str)
+{
+       return str_replace(['-', ' '], '_', strtolower($str));
+}
+
 /** Splits up a string by a space
  * (chr 32)
  *
@@ -14,6 +19,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 +37,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 +54,157 @@ function get_relative_path($filename)
     }
     $relativepath = glue($relativepath,"/");
     return $relativepath;
-}
\ No newline at end of file
+}
+
+/**
+ * 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;
+}
+
+/* Taken from https://www.aviran.org/stripremove-irc-client-control-characters/
+ * We may want to re-base it off our UnrealIRCd's one though.
+ */
+function StripControlCharacters($text)
+{
+    $controlCodes = array(
+        '/(\x03(?:\d{1,2}(?:,\d{1,2})?)?)/',    // Color code
+        '/\x02/',                               // Bold
+        '/\x0F/',                               // Escaped
+        '/\x16/',                               // Italic
+        '/\x1F/'                                // Underline
+    );
+    return preg_replace($controlCodes,'',$text);
+}