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