]>
jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blob - misc/strings.php
5 return str_replace(['-', ' '], '_', strtolower($str));
8 /** Splits up a string by a space
17 function split($str, $delimiter = " ") : Array
19 return explode($delimiter,$str);
25 * @param mixed $delimiter
28 function glue($array, $delimiter = " ")
31 foreach($array as $str)
35 $string .= $str.$delimiter;
37 return trim($string,$delimiter);
41 * Gets the relative path of the filename
42 * @param mixed $filename
45 function get_relative_path($filename)
47 $relativepath = split($filename, "/");
48 foreach($relativepath as &$tok)
50 $isFinal = ($tok == "html") ? 1 : 0;
55 $relativepath = glue($relativepath,"/");
60 * Returns a `nick` if the string was in the syntax:
65 function show_nick_only($str)
67 $x = strpos($str, "!");
69 $str = substr($str, 0, $x);
74 * Figures out how long ago a given time was
75 * returns string example:
80 function how_long_ago($timestamp)
83 $diff = $now - strtotime($timestamp);
94 foreach ($units as $unit => $text) {
95 if ($diff < $unit) continue;
96 $numberOfUnits = floor($diff / $unit);
97 return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'').' ago';
108 function time_of_day()
110 $timeofday = "day"; // in case something went wrong? lol
112 if ($hour >= 18 || $hour < 4)
113 $timeofday = "evening";
114 else if ($hour >= 4 && $hour < 12)
115 $timeofday = "morning";
116 else if ($hour >= 12 && $hour < 18)
117 $timeofday = "afternoon";
124 * Concatenate a string to a string
126 function strcat(&$targ,$string) : void
127 { $targ
.= $
string; }
131 * Concatenate a string to a string and limits the string to a certain length
133 function strlcat(&$targ,$string,$size) : void
135 strcat($targ,$string);
136 $targ = mb_substr($targ,0,$size);
141 * Prefixes a string to a string
143 function strprefix(&$targ,$string) : void
144 { $targ
= $
string.$targ
; }
148 * Prefixes a string to a string and limits the string to a certain length
150 function strlprefix(&$targ,$string,$size) : void
152 if (sizeof($targ) >= $size)
155 strprefix($targ,$string);
156 $targ = mb_substr($targ,0,$size);
159 /** Checks if the token provided is a bad pointer, by reference
160 * Returns Bool value true if it IS bad
168 function BadPtr(&$tok)
170 if (!isset($tok) || empty($tok) || !$tok || strlen($tok) == 0)
175 /** This function takes a string, tokenizes
176 * it by a space (chr 32), removes the first
177 * word/token, and returns the result.
178 * Mostly used for string manipulation around
182 * rparv(String $sentence)
187 function rparv($string)
189 $parv = split($string);
190 $first = strlen($parv[0]) +
1;
191 $string = substr($string, $first);
197 /* Taken from https://www.aviran.org/stripremove-irc-client-control-characters/
198 * We may want to re-base it off our UnrealIRCd's one though.
200 function StripControlCharacters($text)
202 $controlCodes = array(
203 '/(\x03(?:\d{1,2}(?:,\d{1,2})?)?)/', // Color code
207 '/\x1F/' // Underline
209 return preg_replace($controlCodes,'',$text);