1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Comrade42\PhpBBParser\Helper\BBCodes; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class PCREParser |
7
|
|
|
* @package Comrade42\PhpBBParser\Helper\BBCodes |
8
|
|
|
*/ |
9
|
|
|
class PCREParser implements ParserInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected static $patterns = array( |
15
|
|
|
'_' => array( |
16
|
|
|
'/>\s*</sU' => '><', |
17
|
|
|
'/<br>/sU' => PHP_EOL |
18
|
|
|
), |
19
|
|
|
'typeface' => array( |
20
|
|
|
'/<strong>(.*)<\/strong>/sU' => '[b]$1[/b]', |
21
|
|
|
'/<u>(.*)<\/u>/sU' => '[u]$1[/u]', |
22
|
|
|
'/<i>(.*)<\/i>/sU' => '[i]$1[/i]', |
23
|
|
|
'/<strike>(.*)<\/strike>/sU' => '[strike]$1[/strike]' |
24
|
|
|
), |
25
|
|
|
'align' => array( |
26
|
|
|
'/<div align="(.+)">(.*)<\/div>/sU' => '[$1]$2[/$1]' |
27
|
|
|
), |
28
|
|
|
'color' => array( |
29
|
|
|
'/<span style="color:(.+)">(.*)<\/span>/sU' => '[color=$1]$2[/color]' |
30
|
|
|
), |
31
|
|
|
'size' => array( |
32
|
|
|
'/<span style="font-size:(.+)px">(.*)<\/span>/sU' => '[size=$1]$2[/size]' |
33
|
|
|
), |
34
|
|
|
'font' => array( |
35
|
|
|
'/<span style="font-family: (.+)">(.*)<\/span>/sU' => '[font=$1]$2[/font]' |
36
|
|
|
), |
37
|
|
|
'quote' => array( |
38
|
|
|
'/<blockquote><div><cite>(.+) пишет:<\/cite>(.*)<\/div><\/blockquote>/sU' => '[quote="$1"]$2[/quote]', |
39
|
|
|
'/<blockquote><div>(.*)<\/div><\/blockquote>/sU' => '[quote]$1[/quote]' |
40
|
|
|
), |
41
|
|
|
'code' => array( |
42
|
|
|
'/<dl class="codebox"><dt>Код:<\/dt><dd><code>(.*)<\/code><\/dd><\/dl>/sU' => '[code]$1[/code]' |
43
|
|
|
), |
44
|
|
|
'hide' => array( |
45
|
|
|
'/<dl class="codebox hidecode"><dt(?:.*)><\/dt><dd>(.*)<\/dd><\/dl>/sU' => '[hide]$1[/hide]' |
46
|
|
|
), |
47
|
|
|
'list' => array( |
48
|
|
|
'/<ol type="(.+)">(.*)<\/ol>/sU' => '[list=$1]$2[/list]', |
49
|
|
|
'/<ul>(.*)<\/ul>/sU' => '[list]$1[/list]', |
50
|
|
|
'/<li>(.*)<\/li>/sU' => '[*]$1' |
51
|
|
|
), |
52
|
|
|
'link' => array( |
53
|
|
|
'/<a href="(.+)"(?:.*)>(.+)<\/a>/sU' => '[url=$1]$2[/url]', |
54
|
|
|
'/\[url=(?:(https?:\/\/[^\]\s]+)\]?){2}\[\/url\]/sU' => '[url]$1[/url]' |
55
|
|
|
), |
56
|
|
|
'image' => array( |
57
|
|
|
'/<img src="(.+)" style="width: (.+)px;height: (.+)px"(?:.*)>/sU' => '[img($2px,$3px)]$1[/img]', |
58
|
|
|
'/<img src="(.+)"(?:.*)>/sU' => '[img]$1[/img]' |
59
|
|
|
), |
60
|
|
|
'video' => array( |
61
|
|
|
'/<embed (?:.*) src="http:\/\/www\.youtube\.com\/v\/(\w+)"(?:.*)>/sU' => '[youtube]$1[/youtube]', |
62
|
|
|
'/<embed (?:.*) src="http:\/\/www\.dailymotion\.com\/swf\/(\w+)"(?:.*)>/sU' => '[dailymotion]$1[/dailymotion]' |
63
|
|
|
), |
64
|
|
|
'flash' => array( |
65
|
|
|
'/<embed (?:.*) src="(.+)\.swf" width="(.+)" height="(.+)"(?:.*)>/sU' => '[flash($2,$3)]$1.swf[/flash]' |
66
|
|
|
), |
67
|
|
|
'table' => array( |
68
|
|
|
'/<table(?:.*)>(?:.*)<tbody>(.*)<\/tbody>(?:.*)<\/table>/sU' => '[table]$1[/table]', |
69
|
|
|
'/<tr>(.*)<\/tr>/sU' => '[tr]$1[/tr]', |
70
|
|
|
'/<td>(.*)<\/td>/sU' => '[td]$1[/td]' |
71
|
|
|
), |
72
|
|
|
'scrolling' => array( |
73
|
|
|
'/<span><marquee>(.*)<\/marquee><\/span>/sU' => '[scroll]$1[/scroll]', |
74
|
|
|
'/<marquee direction="up"(?:.*)">(.*)<\/marquee>/sU' => '[updown]$1[/updown]' |
75
|
|
|
), |
76
|
|
|
'line' => array( |
77
|
|
|
'/<hr>/sU' => '[hr]' |
78
|
|
|
), |
79
|
|
|
'index' => array( |
80
|
|
|
'/<sub>(.*)<\/sub>/sU' => '[sub]$1[/sub]', |
81
|
|
|
'/<sup>(.*)<\/sup>/sU' => '[sup]$1[/sup]' |
82
|
|
|
), |
83
|
|
|
'effect' => array( |
84
|
|
|
'/<span class="flipV">(.*)<\/span>/sU' => '[flipv]$1[/flipv]', |
85
|
|
|
'/<span class="flipH">(.*)<\/span>/sU' => '[fliph]$1[/fliph]', |
86
|
|
|
'/<span class="(blur|fade)">(.*)<\/span>/sU' => '[$1]$2[/$1]' |
87
|
|
|
), |
88
|
|
|
'random' => array( |
89
|
|
|
'/<dl class="codebox"><dd><strong>Случайное число \(1,(\d+)\)(?:.*)<\/strong>(?:.*)<\/dd><\/dl>/sU' => '[rand]$1[/rand]', |
90
|
|
|
'/<dl class="codebox"><dd><strong>Случайное число \((\d+),(\d+)\)(?:.*)<\/strong>(?:.*)<\/dd><\/dl>/sU' => '[rand]$1,$2[/rand]' |
91
|
|
|
) |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $html |
96
|
|
|
* @param string $section |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
113 |
|
protected static function parse($html, $section) |
100
|
|
|
{ |
101
|
113 |
|
$patterns = static::$patterns[$section]; |
102
|
|
|
|
103
|
113 |
|
if ($section == 'quote') { |
104
|
8 |
|
while (false !== strpos($html, '<blockquote>')) |
105
|
|
|
{ |
106
|
6 |
|
$position = strrpos($html, '<blockquote>'); |
107
|
6 |
|
$_html = preg_replace(array_keys($patterns), $patterns, substr($html, $position)); |
108
|
6 |
|
$html = substr($html, 0, $position) . $_html; |
109
|
6 |
|
} |
110
|
8 |
|
} else { |
111
|
105 |
|
$html = preg_replace(array_keys($patterns), $patterns, $html); |
112
|
|
|
} |
113
|
|
|
|
114
|
113 |
|
return $html; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Parses all BBCode tags in the HTML source at once |
119
|
|
|
* |
120
|
|
|
* @param string $html |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public static function parseAll($html) |
124
|
|
|
{ |
125
|
|
|
foreach (array_keys(static::$patterns) as $section) |
126
|
|
|
{ |
127
|
|
|
$html = static::parse($html, $section); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return trim($html); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Parses [b], [u], [i] and [strike] tags in the HTML source |
135
|
|
|
* |
136
|
|
|
* @param string $html |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
10 |
|
public static function parseTypeface($html) |
140
|
|
|
{ |
141
|
10 |
|
return static::parse($html, 'typeface'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Parses [left], [right], [center] and [justify] tags in the HTML source |
146
|
|
|
* |
147
|
|
|
* @param string $html |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
7 |
|
public static function parseAlign($html) |
151
|
|
|
{ |
152
|
7 |
|
return static::parse($html, 'align'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Parses [color] tag in the HTML source |
157
|
|
|
* |
158
|
|
|
* @param string $html |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
5 |
|
public static function parseColor($html) |
162
|
|
|
{ |
163
|
5 |
|
return static::parse($html, 'color'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Parses [size] tag in the HTML source |
168
|
|
|
* |
169
|
|
|
* @param string $html |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
5 |
|
public static function parseSize($html) |
173
|
|
|
{ |
174
|
5 |
|
return static::parse($html, 'size'); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Parses [font] tag in the HTML source |
179
|
|
|
* |
180
|
|
|
* @param string $html |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
5 |
|
public static function parseFont($html) |
184
|
|
|
{ |
185
|
5 |
|
return static::parse($html, 'font'); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Parses [quote] tag in the HTML source |
190
|
|
|
* |
191
|
|
|
* @param string $html |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
8 |
|
public static function parseQuote($html) |
195
|
|
|
{ |
196
|
8 |
|
return static::parse($html, 'quote'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Parses [code] tag in the HTML source |
201
|
|
|
* |
202
|
|
|
* @param string $html |
203
|
|
|
* @return string |
204
|
|
|
*/ |
205
|
4 |
|
public static function parseCode($html) |
206
|
|
|
{ |
207
|
4 |
|
return static::parse($html, 'code'); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Parses [hide] tag in the HTML source |
212
|
|
|
* |
213
|
|
|
* @param string $html |
214
|
|
|
* @return string |
215
|
|
|
*/ |
216
|
4 |
|
public static function parseHide($html) |
217
|
|
|
{ |
218
|
4 |
|
return static::parse($html, 'hide'); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Parses [list] tag in the HTML source |
223
|
|
|
* |
224
|
|
|
* @param string $html |
225
|
|
|
* @return string |
226
|
|
|
*/ |
227
|
7 |
|
public static function parseList($html) |
228
|
|
|
{ |
229
|
7 |
|
return static::parse($html, 'list'); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Parses [url] tag in the HTML source |
234
|
|
|
* |
235
|
|
|
* @param string $html |
236
|
|
|
* @return string |
237
|
|
|
*/ |
238
|
8 |
|
public static function parseLink($html) |
239
|
|
|
{ |
240
|
8 |
|
return static::parse($html, 'link'); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Parses [img] tag in the HTML source |
245
|
|
|
* |
246
|
|
|
* @param string $html |
247
|
|
|
* @return string |
248
|
|
|
*/ |
249
|
5 |
|
public static function parseImage($html) |
250
|
|
|
{ |
251
|
5 |
|
return static::parse($html, 'image'); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Parses [youtube] and [dailymotion] tags in the HTML source |
256
|
|
|
* |
257
|
|
|
* @param string $html |
258
|
|
|
* @return string |
259
|
|
|
*/ |
260
|
6 |
|
public static function parseVideo($html) |
261
|
|
|
{ |
262
|
6 |
|
return static::parse($html, 'video'); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Parses [flash] and [embed-flash] tags in the HTML source |
267
|
|
|
* |
268
|
|
|
* @param string $html |
269
|
|
|
* @return string |
270
|
|
|
*/ |
271
|
4 |
|
public static function parseFlash($html) |
272
|
|
|
{ |
273
|
4 |
|
return static::parse($html, 'flash'); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Parses [table] tag in the HTML source |
278
|
|
|
* |
279
|
|
|
* @param string $html |
280
|
|
|
* @return string |
281
|
|
|
*/ |
282
|
4 |
|
public static function parseTable($html) |
283
|
|
|
{ |
284
|
4 |
|
return static::parse($html, 'table'); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Parses [scroll] and [updown] tags in the HTML source |
289
|
|
|
* |
290
|
|
|
* @param string $html |
291
|
|
|
* @return string |
292
|
|
|
*/ |
293
|
6 |
|
public static function parseScrolling($html) |
294
|
|
|
{ |
295
|
6 |
|
return static::parse($html, 'scrolling'); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Parses [hr] tag in the HTML source |
300
|
|
|
* |
301
|
|
|
* @param string $html |
302
|
|
|
* @return string |
303
|
|
|
*/ |
304
|
4 |
|
public static function parseLine($html) |
305
|
|
|
{ |
306
|
4 |
|
return static::parse($html, 'line'); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Parses [sub] and [sup] tags in the HTML source |
311
|
|
|
* |
312
|
|
|
* @param string $html |
313
|
|
|
* @return string |
314
|
|
|
*/ |
315
|
6 |
|
public static function parseIndex($html) |
316
|
|
|
{ |
317
|
6 |
|
return static::parse($html, 'index'); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Parses [flipv], [fliph], [blur] and [fade] tags in the HTML source |
322
|
|
|
* |
323
|
|
|
* @param string $html |
324
|
|
|
* @return string |
325
|
|
|
*/ |
326
|
10 |
|
public static function parseEffect($html) |
327
|
|
|
{ |
328
|
10 |
|
return static::parse($html, 'effect'); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Parses [rand] tag in the HTML source |
333
|
|
|
* |
334
|
|
|
* @param string $html |
335
|
|
|
* @return string |
336
|
|
|
*/ |
337
|
5 |
|
public static function parseRandom($html) |
338
|
|
|
{ |
339
|
5 |
|
return static::parse($html, 'random'); |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|