]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blob - misc/strings.php
Fix "last login" meta and display on PanelUsers page
[irc/unrealircd/unrealircd-webpanel.git] / misc / strings.php
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 */
12 function split($str, $delimiter = " ") : Array
13 {
14 return explode($delimiter,$str);
15 }
16
17 /**
18 *
19 * @param mixed $array
20 * @param mixed $delimiter
21 * @return string
22 */
23 function 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
35 /**
36 * Gets the relative path of the filename
37 * @param mixed $filename
38 * @return string
39 */
40 function 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;
52 }
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 */
60 function show_nick_only($str)
61 {
62 $x = strpos($str, "!");
63 if ($x !== false)
64 $str = substr($str, 0, $x);
65 return $str;
66 }
67
68
69 function how_long_ago($timestamp)
70 {
71 $now = time();
72 $diff = $now - strtotime($timestamp);
73 $units = array(
74 31536000 => 'year',
75 2592000 => 'month',
76 604800 => 'week',
77 86400 => 'day',
78 3600 => 'hour',
79 60 => 'minute',
80 1 => 'second'
81 );
82
83 foreach ($units as $unit => $text) {
84 if ($diff < $unit) continue;
85 $numberOfUnits = floor($diff / $unit);
86 return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'').' ago';
87 }
88 }