]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blame - misc/strings.php
Logs: show search pane (on desktop)
[irc/unrealircd/unrealircd-webpanel.git] / misc / strings.php
CommitLineData
fe2a6f27
VP
1<?php
2
4b48b46f
VP
3function to_slug($str)
4{
5 return str_replace(['-', ' '], '_', strtolower($str));
6}
7
fe2a6f27
VP
8/** Splits up a string by a space
9 * (chr 32)
10 *
11 * Syntax:
12 * split($string)
13 *
14 * Returns:
15 * array $tokens
16 */
17function split($str, $delimiter = " ") : Array
18{
19 return explode($delimiter,$str);
20}
21
33f512fa
VP
22/**
23 *
24 * @param mixed $array
25 * @param mixed $delimiter
26 * @return string
27 */
fe2a6f27
VP
28function glue($array, $delimiter = " ")
29{
30 $string = "";
31 foreach($array as $str)
32 {
33 if (!$str)
34 continue;
35 $string .= $str.$delimiter;
36 }
37 return trim($string,$delimiter);
38}
39
33f512fa
VP
40/**
41 * Gets the relative path of the filename
42 * @param mixed $filename
43 * @return string
44 */
fe2a6f27
VP
45function get_relative_path($filename)
46{
47 $relativepath = split($filename, "/");
48 foreach($relativepath as &$tok)
49 {
50 $isFinal = ($tok == "html") ? 1 : 0;
51 $tok = NULL;
52 if ($isFinal)
53 break;
54 }
55 $relativepath = glue($relativepath,"/");
56 return $relativepath;
62d4ea03 57}
33f512fa
VP
58
59/**
60 * Returns a `nick` if the string was in the syntax:
61 * nick!ident@host
62 * @param mixed $str
63 * @return mixed
64 */
65function show_nick_only($str)
66{
67 $x = strpos($str, "!");
68 if ($x !== false)
69 $str = substr($str, 0, $x);
70 return $str;
71}
2d62c85d 72
f3d53a31
VP
73/**
74 * Figures out how long ago a given time was
75 * returns string example:
76 * - "32 minutes ago"
77 * - "5 hours ago"
78 * - "12 seconds ago"
79 */
2d62c85d
VP
80function how_long_ago($timestamp)
81{
82 $now = time();
83 $diff = $now - strtotime($timestamp);
2d62c85d
VP
84 $units = array(
85 31536000 => 'year',
86 2592000 => 'month',
87 604800 => 'week',
88 86400 => 'day',
89 3600 => 'hour',
90 60 => 'minute',
91 1 => 'second'
92 );
93
94 foreach ($units as $unit => $text) {
95 if ($diff < $unit) continue;
96 $numberOfUnits = floor($diff / $unit);
97 return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'').' ago';
98 }
119fee10
VP
99}
100
f3d53a31
VP
101/**
102 * Uses system time.
103 * Returns:
104 * - evening
105 * - morning
106 * - afternoon
107 */
119fee10
VP
108function time_of_day()
109{
110 $timeofday = "day"; // in case something went wrong? lol
111 $hour = date("H");
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";
118
119 return $timeofday;
f3d53a31
VP
120}
121
122
123/**
124 * Concatenate a string to a string
125 */
126function strcat(&$targ,$string) : void
127{ $targ .= $string; }
128
129
130/**
131 * Concatenate a string to a string and limits the string to a certain length
132 */
133function strlcat(&$targ,$string,$size) : void
134{
135 strcat($targ,$string);
136 $targ = mb_substr($targ,0,$size);
137}
138
139
140/**
141 * Prefixes a string to a string
142 */
143function strprefix(&$targ,$string) : void
144{ $targ = $string.$targ; }
145
146
147/**
148 * Prefixes a string to a string and limits the string to a certain length
149 */
150function strlprefix(&$targ,$string,$size) : void
151{
152 if (sizeof($targ) >= $size)
153 return;
154
155 strprefix($targ,$string);
156 $targ = mb_substr($targ,0,$size);
157}
158
159/** Checks if the token provided is a bad pointer, by reference
160 * Returns Bool value true if it IS bad
161 *
162 * Syntax:
163 * BadPtr($variable)
164 *
165 * Returns:
166 * @
167*/
168function BadPtr(&$tok)
169{
170 if (!isset($tok) || empty($tok) || !$tok || strlen($tok) == 0)
171 return true;
172 return false;
d2db68e3
VP
173}
174
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
179 * the source.
180 *
181 * Syntax:
182 * rparv(String $sentence)
183 *
184 * Returns:
185 * string|false
186 */
187function rparv($string)
188{
189 $parv = split($string);
190 $first = strlen($parv[0]) + 1;
191 $string = substr($string, $first);
192 if ($string)
193 return $string;
194 return false;
064843a8
BM
195}
196
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.
199 */
200function StripControlCharacters($text)
201{
202 $controlCodes = array(
203 '/(\x03(?:\d{1,2}(?:,\d{1,2})?)?)/', // Color code
204 '/\x02/', // Bold
205 '/\x0F/', // Escaped
206 '/\x16/', // Italic
207 '/\x1F/' // Underline
208 );
209 return preg_replace($controlCodes,'',$text);
210}