1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mos\TextFilter; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Utilities when working with text. |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
trait TTextUtilities |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Get text until <!--stop--> or all text. |
13
|
|
|
* |
14
|
|
|
* @param string $text with content |
15
|
|
|
* |
16
|
|
|
* @return string with text |
17
|
|
|
*/ |
18
|
8 |
|
public function getUntilStop($text) |
19
|
|
|
{ |
20
|
8 |
|
$pos = stripos($text, "<!--stop-->"); |
21
|
8 |
|
if ($pos) { |
22
|
2 |
|
$text = substr($text, 0, $pos); |
23
|
2 |
|
} |
24
|
8 |
|
return $text; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get text until <!--more--> or all text. |
31
|
|
|
* |
32
|
|
|
* @param string $text with content |
33
|
|
|
* |
34
|
|
|
* @return array with text and boolean if more was detected. |
35
|
|
|
*/ |
36
|
8 |
|
public function getUntilMore($text) |
37
|
|
|
{ |
38
|
8 |
|
$pos = stripos($text, "<!--more-->"); |
39
|
8 |
|
$hasMore = $pos; |
40
|
8 |
|
if ($pos) { |
41
|
1 |
|
$text = substr($text, 0, $pos); |
42
|
1 |
|
} |
43
|
8 |
|
return [$text, $hasMore]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Wrap HTML element with with start and end. |
50
|
|
|
* |
51
|
|
|
* @param string $text with content |
52
|
|
|
* @param string $tag HTML tag to search for |
53
|
|
|
* @param string $start wrap start part |
54
|
|
|
* @param string $end wrap end part |
55
|
|
|
* @param number $count hits to search for |
56
|
|
|
* |
57
|
|
|
* @return array with text and boolean if more was detected. |
58
|
|
|
*/ |
59
|
|
|
public function wrapElementWithStartEnd($text, $tag, $start, $end, $count) |
60
|
|
|
{ |
61
|
|
|
return preg_replace( |
62
|
|
|
"#(<$tag>)(.*?)(</$tag>)#", |
63
|
|
|
"$start$1$2$3$end</a>", |
64
|
|
|
$text, |
65
|
|
|
$count |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Wrap content of a HTML element with start and end. |
73
|
|
|
* |
74
|
|
|
* @param string $text with content |
75
|
|
|
* @param string $tag HTML tag to search for |
76
|
|
|
* @param string $start wrap start part |
77
|
|
|
* @param string $end wrap end part |
78
|
|
|
* @param number $count hits to search for |
79
|
|
|
* |
80
|
|
|
* @return array with text and boolean if more was detected. |
81
|
|
|
*/ |
82
|
|
|
public function wrapElementContentWithStartEnd($text, $tag, $start, $end, $count) |
83
|
|
|
{ |
84
|
|
|
return preg_replace( |
85
|
|
|
"#(<$tag>)(.*?)(</$tag>)#", |
86
|
|
|
"$1$start$2$end$3</a>", |
87
|
|
|
$text, |
88
|
|
|
$count |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Create a TOC of HTML headings from and to a certain level. |
96
|
|
|
* |
97
|
|
|
* @param string $text with content |
98
|
|
|
* @param integer $start level of headings to use for toc. |
99
|
|
|
* @param integer $stop level of headings to use for toc. |
100
|
|
|
* |
101
|
|
|
* @return array with entries to generate a TOC. |
102
|
|
|
*/ |
103
|
|
|
public function createToc($text, $start = 2, $stop = 4) |
104
|
|
|
{ |
105
|
|
|
$level = "$start-$stop"; |
106
|
|
|
$pattern = "/<(h[$level])([^>]*)>(.*)<\/h[$level]>/"; |
107
|
|
|
preg_match_all($pattern, $text, $matches, PREG_SET_ORDER); |
108
|
|
|
|
109
|
|
|
$toc = []; |
110
|
|
|
foreach ($matches as $val) { |
|
|
|
|
111
|
|
|
preg_match("/id=['\"]([^>\"']+)/", $val[2], $id); |
112
|
|
|
$id = isset($id[1]) ? $id[1] : null; |
113
|
|
|
var_dump($matches[1]); |
|
|
|
|
114
|
|
|
$toc[] = [ |
115
|
|
|
"level" => isset($val[1]) |
116
|
|
|
? $val[1] |
117
|
|
|
: null, |
118
|
|
|
"title" => isset($val[3]) |
119
|
|
|
? $val[3] |
120
|
|
|
: null, |
121
|
|
|
"id" => $id, |
122
|
|
|
]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $toc; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get content as pure text. |
132
|
|
|
* |
133
|
|
|
* @return string with the pure text. |
134
|
|
|
*/ |
135
|
|
|
/* public function GetPureText() { |
136
|
|
|
return preg_replace('/\s+/', ' ', strip_tags($this->GetFilteredData())); |
137
|
|
|
} |
138
|
|
|
*/ |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Returns the excerpt of the text with at most the specified amount of characters. |
144
|
|
|
* |
145
|
|
|
* @param int $chars the number of characters to return. |
146
|
|
|
* @param boolean $hard do a hard break at exactly $chars characters or find closest space. |
147
|
|
|
* @return string as the excerpt. |
148
|
|
|
*/ |
149
|
|
|
/* public function GetExcerpt($chars=139, $hard=false) { |
150
|
|
|
if(!isset($this->data['data_filtered'])) { |
151
|
|
|
return null; |
152
|
|
|
} |
153
|
|
|
$excerpt = strip_tags($this->data['data_filtered']); |
154
|
|
|
|
155
|
|
|
if(strlen($excerpt) > $chars) { |
156
|
|
|
$excerpt = substr($excerpt, 0, $chars-1); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if(!$hard) { |
160
|
|
|
$lastSpace = strrpos($excerpt, ' '); |
161
|
|
|
$excerpt = substr($excerpt, 0, $lastSpace); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $excerpt; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Returns the first paragraph ot the text. |
170
|
|
|
* |
171
|
|
|
* @return string as the first paragraph. |
172
|
|
|
*/ |
173
|
|
|
/* public function GetFirstParagraph() { |
174
|
|
|
if(!isset($this->data['data_filtered'])) { |
175
|
|
|
return null; |
176
|
|
|
} |
177
|
|
|
$excerpt = $this->data['data_filtered']; |
178
|
|
|
|
179
|
|
|
$firstPara = strpos($excerpt, '</p>'); |
180
|
|
|
$excerpt = substr($excerpt, 0, $firstPara + 4); |
181
|
|
|
|
182
|
|
|
return $excerpt; |
183
|
|
|
} |
184
|
|
|
*/ |
185
|
|
|
} |
186
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.