1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Core\Traits; |
4
|
|
|
|
5
|
|
|
use Core\Constant; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* a trait with some string related helpers |
9
|
|
|
* Trait StringFunctions |
10
|
|
|
* @package Core\Traits |
11
|
|
|
*/ |
12
|
|
|
trait StringFunctions |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* does a haystack start with a needle ? |
16
|
|
|
* @param $haystack string the string to search in |
17
|
|
|
* @param $needle string the string to search for |
18
|
|
|
* @return bool |
19
|
|
|
*/ |
20
|
|
|
public function startsWith(string $haystack, string $needle): bool |
21
|
|
|
{ |
22
|
|
|
$length = strlen($needle); |
23
|
|
|
return (substr($haystack, 0, $length) === $needle); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Does a haystack end with a needle |
28
|
|
|
* @param $haystack string the string to search in |
29
|
|
|
* @param $needle string the string to search for |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
|
|
public function endsWith(string $haystack, string $needle): bool |
33
|
|
|
{ |
34
|
|
|
$length = strlen($needle); |
35
|
|
|
if ($length == 0) { |
36
|
|
|
return true; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return (substr($haystack, -$length) === $needle); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Remove the tail of a string |
44
|
|
|
* @param $string string to slice apart |
45
|
|
|
* @param $tail string the string to remove |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
public function removeFromEnd(string $string, string $tail): string |
49
|
|
|
{ |
50
|
|
|
if ($this->endsWith($string, $tail)) { |
51
|
|
|
$string = substr($string, 0, -strlen($tail)); |
52
|
|
|
} |
53
|
|
|
return $string; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* remove some characters from the front of the string |
58
|
|
|
* @param string $string the string to be decapitated |
59
|
|
|
* @param string $head the head to be cut off ("OFF WITH HIS HEAD") |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function removeFromBeginning(string $string, string $head): string |
63
|
|
|
{ |
64
|
|
|
if ($this->startsWith($string, $head)) { |
65
|
|
|
$string = substr($string, strlen($head)); |
66
|
|
|
} |
67
|
|
|
return $string; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* create an excerpt, shortening the text to a specific number of words |
72
|
|
|
* @param string $text the text to shorten |
73
|
|
|
* @param int $count number of words |
74
|
|
|
* @return string the shortend text |
75
|
|
|
* @throws \ErrorException |
76
|
|
|
*/ |
77
|
|
|
public function getExcerpt(string $text, int $count = Constant::EXCERPT_WORD_COUNT):string |
78
|
|
|
{ |
79
|
|
|
if ($count < 1) { |
80
|
|
|
throw new \ErrorException('excerpt length too low'); |
81
|
|
|
} |
82
|
|
|
$text = str_replace(" ", " ", $text); |
83
|
|
|
|
84
|
|
|
//Searching for the page break tag |
85
|
|
|
$breakTagPosition = strpos($text, "<!-- EndOfExcerptBlogOc -->"); |
86
|
|
|
if($breakTagPosition > 0){ |
87
|
|
|
return $this->completeDom(substr($text, 0, $breakTagPosition)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
//exploding on space except for in img, p and span. |
91
|
|
|
$string = preg_split('/(<img[^>]+\>)|(<p[^>]+\>)|(<span[^>]+\>)|\s/', $text, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); |
92
|
|
|
|
93
|
|
|
//The preg split can return false, probably will never happen but just in case. |
94
|
|
|
if(!$string) |
95
|
|
|
{ |
96
|
|
|
throw new \Error("excerpt generation failed"); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (count($string) <= $count) { |
100
|
|
|
return $text; |
101
|
|
|
} |
102
|
|
|
$trimed = ''; |
103
|
|
|
for ($wordCounter = 0; $wordCounter < $count; $wordCounter++) { |
104
|
|
|
$trimed .= $string[$wordCounter]; |
105
|
|
|
if ($wordCounter < $count - 1) { |
106
|
|
|
$trimed .= " "; |
107
|
|
|
} else { |
108
|
|
|
$trimed .= "[...]"; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this->completeDom($trimed); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Close the dom in given $text |
117
|
|
|
* @param string $text unclean html |
118
|
|
|
* @return string cleaned up html |
119
|
|
|
*/ |
120
|
|
|
private function completeDom(string $text): string |
121
|
|
|
{ |
122
|
|
|
//grabbed from https://gist.github.com/JayWood/348752b568ecd63ae5ce#gistcomment-2310550 |
123
|
|
|
libxml_use_internal_errors(true); |
124
|
|
|
|
125
|
|
|
$dom = new \DOMDocument; |
126
|
|
|
$dom->loadHTML($text); |
127
|
|
|
|
128
|
|
|
// Strip wrapping <html> and <body> tags |
129
|
|
|
$mock = new \DOMDocument; |
130
|
|
|
$body = $dom->getElementsByTagName('body')->item(0); |
131
|
|
|
foreach ($body->childNodes as $child) { |
132
|
|
|
$mock->appendChild($mock->importNode($child, true)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return trim($mock->saveHTML()); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* check passed string, returns true if the string contains alphaNum or _ or -. use for checking database tables, column names or slugs |
140
|
|
|
* @param string $string the string to analyse |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
|
|
public function isAlphaNum(string $string): bool |
144
|
|
|
{ |
145
|
|
|
return preg_match("/^[A-Za-z0-9_-]+$/", $string); |
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* check if each string in passed array is valid |
150
|
|
|
* @param array $strings |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
|
|
public function isAllAlphaNum(array $strings):bool |
154
|
|
|
{ |
155
|
|
|
$result = true; |
156
|
|
|
foreach ($strings as $string) |
157
|
|
|
{ |
158
|
|
|
if(!$this->isAlphaNum($string)) |
159
|
|
|
{ |
160
|
|
|
$result = false; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
return $result; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* check is a string is hexadecimal |
168
|
|
|
* @param string $string |
169
|
|
|
* @return false|int |
170
|
|
|
*/ |
171
|
|
|
public function isHexa(string $string):bool |
172
|
|
|
{ |
173
|
|
|
return preg_match("/[\da-f]/",$string); |
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* check if the sent var is an integer |
178
|
|
|
* @param $int |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
|
|
public function isInt($int):bool |
182
|
|
|
{ |
183
|
|
|
return filter_var($int, FILTER_VALIDATE_INT); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Verify if a string is a valid email |
188
|
|
|
* @param string $email |
189
|
|
|
* @return mixed |
190
|
|
|
*/ |
191
|
|
|
public function isEmail(string $email) |
192
|
|
|
{ |
193
|
|
|
return filter_var($email, FILTER_VALIDATE_EMAIL); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
} |