|
1
|
|
|
<?php |
|
2
|
|
|
class Nip_Helper_Strings extends Nip\Helpers\AbstractHelper { |
|
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Limits a string to a certain number of words |
|
6
|
|
|
* |
|
7
|
|
|
* @param string $string |
|
8
|
|
|
* @param int $limit |
|
9
|
|
|
* @param string $end |
|
10
|
|
|
* @return string |
|
11
|
|
|
*/ |
|
12
|
|
|
public function limitWords($string, $limit = false, $end = '...') { |
|
13
|
|
|
$words = explode(" ", $string); |
|
14
|
|
|
|
|
15
|
|
|
if (count($words) <= $limit) { |
|
16
|
|
|
return $string; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
$return = []; |
|
20
|
|
|
for ($i = 0; $i < $limit; $i++) { |
|
21
|
|
|
$return[] = $words[$i]; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
$return[] = $end; |
|
25
|
|
|
|
|
26
|
|
|
return implode(" ", $return); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Injects GET params in links |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $string |
|
34
|
|
|
* @param array $params |
|
35
|
|
|
* @return string |
|
36
|
|
|
*/ |
|
37
|
|
|
public function injectParams($string, $params = array()) { |
|
38
|
|
|
$links = preg_split('#(<a\b[^>]+>)#', $string, -1, PREG_SPLIT_DELIM_CAPTURE); |
|
39
|
|
|
$old = $links; |
|
40
|
|
|
|
|
41
|
|
|
foreach ($links as &$match) { |
|
42
|
|
|
if (preg_match('/<a\b/', $match) && !preg_match('/(?:#|mailto)/', $match)) { |
|
43
|
|
|
preg_match('/^([^"]+")([^"]+)/', $match, $matches); |
|
44
|
|
|
if ($matches) { |
|
|
|
|
|
|
45
|
|
|
$link = html_entity_decode($matches[2]); |
|
46
|
|
|
if (strpos($link, "?") === false) { |
|
47
|
|
|
$link .= "?"; |
|
48
|
|
|
} else { |
|
49
|
|
|
$link .= "&"; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$link .= http_build_query($params); |
|
53
|
|
|
|
|
54
|
|
|
$match = str_replace($matches[2], $link, $match); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$string = str_replace($old, $links, $string); |
|
60
|
|
|
return $string; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Converts all relative hrefs and image srcs to absolute |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $string |
|
68
|
|
|
* @param string $base |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
|
|
public function relativeToAbsolute($string, $base) { |
|
72
|
|
|
$matches = preg_split('#(<(a|img)\b[^>]+>)#', $string, -1, PREG_SPLIT_DELIM_CAPTURE); |
|
73
|
|
|
$old = $matches; |
|
74
|
|
|
|
|
75
|
|
|
foreach ($matches as &$match) { |
|
76
|
|
|
if (preg_match('/<(a|img)\b/', $match) && !preg_match('/(?:http|#|mailto)/', $match)) { |
|
77
|
|
|
$match = preg_replace('/^([^"]+")([^"]+)/', '$1'.$base.'$2', $match); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$string = str_replace($old, $matches, $string); |
|
82
|
|
|
return $string; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function moneyFormat($number) |
|
86
|
|
|
{ |
|
87
|
|
|
return money_format('%n', $number); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function cronoTimeInSeconds($time) |
|
91
|
|
|
{ |
|
92
|
|
|
$parts = explode(':', $time); |
|
93
|
|
|
$seconds = array_pop($parts); |
|
94
|
|
|
$minutes = array_pop($parts); |
|
95
|
|
|
$hours = array_pop($parts); |
|
96
|
|
|
$days = array_pop($parts); |
|
97
|
|
|
|
|
98
|
|
|
return (($days*24 + $hours)*60 + $minutes)*60 + $seconds; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function secondsInCronoTime($seconds) |
|
102
|
|
|
{ |
|
103
|
|
View Code Duplication |
if ($days = intval((floor($seconds / 86400)))) { |
|
|
|
|
|
|
104
|
|
|
$seconds = $seconds - $days*86400; |
|
105
|
|
|
$return .= ($return ? ':' : '') . str_pad($days, 2, 0,STR_PAD_LEFT); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
View Code Duplication |
if ($hours = intval((floor($seconds / 3600))) OR $return) { |
|
|
|
|
|
|
108
|
|
|
$seconds = $seconds - $hours*3600; |
|
109
|
|
|
$return .= ($return ? ':' : '') . str_pad($hours, 2, 0,STR_PAD_LEFT); |
|
110
|
|
|
} |
|
111
|
|
View Code Duplication |
if ($minutes = intval((floor($seconds / 60))) OR $return) { |
|
|
|
|
|
|
112
|
|
|
$seconds = $seconds - $minutes*60; |
|
113
|
|
|
$return .= ($return ? ':' : '') . str_pad($minutes, 2, 0, STR_PAD_LEFT); |
|
114
|
|
|
} |
|
115
|
|
|
$seconds = round($seconds, 2); |
|
116
|
|
|
$return .= ($return ? ':' : '') . str_pad($seconds, 2, 0, STR_PAD_LEFT); |
|
117
|
|
|
|
|
118
|
|
|
return $return; |
|
119
|
|
|
} |
|
120
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.