]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blame - misc/strings.php
UNFINISHED UnrealIRCd config parsing
[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
VP
67
68
69function how_long_ago($timestamp)
70{
71 $now = time();
72 $diff = $now - strtotime($timestamp);
2d62c85d
VP
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 }
119fee10
VP
88}
89
90function time_of_day()
91{
92 $timeofday = "day"; // in case something went wrong? lol
93 $hour = date("H");
94 if ($hour >= 18 || $hour < 4)
95 $timeofday = "evening";
96 else if ($hour >= 4 && $hour < 12)
97 $timeofday = "morning";
98 else if ($hour >= 12 && $hour < 18)
99 $timeofday = "afternoon";
100
101 return $timeofday;
2d62c85d 102}