1
|
|
|
<?php namespace Golonka\BBCode; |
2
|
|
|
|
3
|
|
|
use \Golonka\BBCode\Traits\ArrayTrait; |
4
|
|
|
|
5
|
|
|
class BBCodeParser |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
use ArrayTrait; |
9
|
|
|
|
10
|
|
|
public $parsers = [ |
11
|
|
|
'bold' => [ |
12
|
|
|
'pattern' => '/\[b\](.*?)\[\/b\]/s', |
13
|
|
|
'replace' => '<strong>$1</strong>', |
14
|
|
|
'content' => '$1' |
15
|
|
|
], |
16
|
|
|
'italic' => [ |
17
|
|
|
'pattern' => '/\[i\](.*?)\[\/i\]/s', |
18
|
|
|
'replace' => '<em>$1</em>', |
19
|
|
|
'content' => '$1' |
20
|
|
|
], |
21
|
|
|
'underline' => [ |
22
|
|
|
'pattern' => '/\[u\](.*?)\[\/u\]/s', |
23
|
|
|
'replace' => '<u>$1</u>', |
24
|
|
|
'content' => '$1' |
25
|
|
|
], |
26
|
|
|
'linethrough' => [ |
27
|
|
|
'pattern' => '/\[s\](.*?)\[\/s\]/s', |
28
|
|
|
'replace' => '<strike>$1</strike>', |
29
|
|
|
'content' => '$1' |
30
|
|
|
], |
31
|
|
|
'size' => [ |
32
|
|
|
'pattern' => '/\[size\=([1-7])\](.*?)\[\/size\]/s', |
33
|
|
|
'replace' => '<font size="$1">$2</font>', |
34
|
|
|
'content' => '$2' |
35
|
|
|
], |
36
|
|
|
'color' => [ |
37
|
|
|
'pattern' => '/\[color\=(#[A-f0-9]{6}|#[A-f0-9]{3})\](.*?)\[\/color\]/s', |
38
|
|
|
'replace' => '<font color="$1">$2</font>', |
39
|
|
|
'content' => '$2' |
40
|
|
|
], |
41
|
|
|
'center' => [ |
42
|
|
|
'pattern' => '/\[center\](.*?)\[\/center\]/s', |
43
|
|
|
'replace' => '<div style="text-align:center;">$1</div>', |
44
|
|
|
'content' => '$1' |
45
|
|
|
], |
46
|
|
|
'left' => [ |
47
|
|
|
'pattern' => '/\[left\](.*?)\[\/left\]/s', |
48
|
|
|
'replace' => '<div style="text-align:left;">$1</div>', |
49
|
|
|
'content' => '$1' |
50
|
|
|
], |
51
|
|
|
'right' => [ |
52
|
|
|
'pattern' => '/\[right\](.*?)\[\/right\]/s', |
53
|
|
|
'replace' => '<div style="text-align:right;">$1</div>', |
54
|
|
|
'content' => '$1' |
55
|
|
|
], |
56
|
|
|
'quote' => [ |
57
|
|
|
'pattern' => '/\[quote\](.*?)\[\/quote\]/s', |
58
|
|
|
'replace' => '<blockquote>$1</blockquote>', |
59
|
|
|
'content' => '$1' |
60
|
|
|
], |
61
|
|
|
'namedquote' => [ |
62
|
|
|
'pattern' => '/\[quote\=(.*?)\](.*)\[\/quote\]/s', |
63
|
|
|
'replace' => '<blockquote><small>$1</small>$2</blockquote>', |
64
|
|
|
'content' => '$2' |
65
|
|
|
], |
66
|
|
|
'link' => [ |
67
|
|
|
'pattern' => '/\[url\](.*?)\[\/url\]/s', |
68
|
|
|
'replace' => '<a href="$1">$1</a>', |
69
|
|
|
'content' => '$1' |
70
|
|
|
], |
71
|
|
|
'namedlink' => [ |
72
|
|
|
'pattern' => '/\[url\=(.*?)\](.*?)\[\/url\]/s', |
73
|
|
|
'replace' => '<a href="$1">$2</a>', |
74
|
|
|
'content' => '$2' |
75
|
|
|
], |
76
|
|
|
'image' => [ |
77
|
|
|
'pattern' => '/\[img\](.*?)\[\/img\]/s', |
78
|
|
|
'replace' => '<img src="$1">', |
79
|
|
|
'content' => '$1' |
80
|
|
|
], |
81
|
|
|
'orderedlistnumerical' => [ |
82
|
|
|
'pattern' => '/\[list=1\](.*?)\[\/list\]/s', |
83
|
|
|
'replace' => '<ol>$1</ol>', |
84
|
|
|
'content' => '$1' |
85
|
|
|
], |
86
|
|
|
'orderedlistalpha' => [ |
87
|
|
|
'pattern' => '/\[list=a\](.*?)\[\/list\]/s', |
88
|
|
|
'replace' => '<ol type="a">$1</ol>', |
89
|
|
|
'content' => '$1' |
90
|
|
|
], |
91
|
|
|
'unorderedlist' => [ |
92
|
|
|
'pattern' => '/\[list\](.*?)\[\/list\]/s', |
93
|
|
|
'replace' => '<ul>$1</ul>', |
94
|
|
|
'content' => '$1' |
95
|
|
|
], |
96
|
|
|
'listitem' => [ |
97
|
|
|
'pattern' => '/\[\*\](.*)/', |
98
|
|
|
'replace' => '<li>$1</li>', |
99
|
|
|
'content' => '$1' |
100
|
|
|
], |
101
|
|
|
'code' => [ |
102
|
|
|
'pattern' => '/\[code\](.*?)\[\/code\]/s', |
103
|
|
|
'replace' => '<code>$1</code>', |
104
|
|
|
'content' => '$1' |
105
|
|
|
], |
106
|
|
|
'youtube' => [ |
107
|
|
|
'pattern' => '/\[youtube\](.*?)\[\/youtube\]/s', |
108
|
|
|
'replace' => '<iframe width="560" height="315" src="//www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>', |
109
|
|
|
'content' => '$1' |
110
|
|
|
], |
111
|
|
|
'linebreak' => [ |
112
|
|
|
'pattern' => '/\r\n/', |
113
|
|
|
'replace' => '<br />', |
114
|
|
|
'content' => '' |
115
|
|
|
], |
116
|
|
|
'sub' => [ |
117
|
|
|
'pattern' => '/\[sub\](.*?)\[\/sub\]/s', |
118
|
|
|
'replace' => '<sub>$1</sub>', |
119
|
|
|
'content' => '$1' |
120
|
|
|
], |
121
|
|
|
'sup' => [ |
122
|
|
|
'pattern' => '/\[sup\](.*?)\[\/sup\]/s', |
123
|
|
|
'replace' => '<sup>$1</sup>', |
124
|
|
|
'content' => '$1' |
125
|
|
|
], |
126
|
|
|
'small' => [ |
127
|
|
|
'pattern' => '/\[small\](.*?)\[\/small\]/s', |
128
|
|
|
'replace' => '<small>$1</small>', |
129
|
|
|
'content' => '$1' |
130
|
|
|
] |
131
|
|
|
]; |
132
|
|
|
|
133
|
|
|
private $enabledParsers; |
134
|
|
|
|
135
|
|
|
public function __construct() |
136
|
|
|
{ |
137
|
|
|
$this->enabledParsers = $this->parsers; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Parses the BBCode string |
142
|
|
|
* @param string $source String containing the BBCode |
143
|
|
|
* @return string Parsed string |
144
|
|
|
*/ |
145
|
|
|
public function parse($source, $caseInsensitive = false) |
146
|
|
|
{ |
147
|
|
|
foreach ($this->enabledParsers as $name => $parser) { |
148
|
|
|
$pattern = ($caseInsensitive) ? $parser['pattern'].'i' : $parser['pattern']; |
149
|
|
|
|
150
|
|
|
$source = $this->searchAndReplace($pattern, $parser['replace'], $source); |
151
|
|
|
} |
152
|
|
|
return $source; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Remove all BBCode |
157
|
|
|
* @param string $source |
158
|
|
|
* @return string Parsed text |
159
|
|
|
*/ |
160
|
|
|
public function stripBBCodeTags($source) |
161
|
|
|
{ |
162
|
|
|
foreach ($this->parsers as $name => $parser) { |
163
|
|
|
$source = $this->searchAndReplace($parser['pattern'].'i', $parser['content'], $source); |
164
|
|
|
} |
165
|
|
|
return $source; |
166
|
|
|
} |
167
|
|
|
/** |
168
|
|
|
* Searches after a specified pattern and replaces it with provided structure |
169
|
|
|
* @param string $pattern Search pattern |
170
|
|
|
* @param string $replace Replacement structure |
171
|
|
|
* @param string $source Text to search in |
172
|
|
|
* @return string Parsed text |
173
|
|
|
*/ |
174
|
|
|
protected function searchAndReplace($pattern, $replace, $source) |
175
|
|
|
{ |
176
|
|
|
while (preg_match($pattern, $source)) { |
177
|
|
|
$source = preg_replace($pattern, $replace, $source); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $source; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Helper function to parse case sensitive |
185
|
|
|
* @param string $source String containing the BBCode |
186
|
|
|
* @return string Parsed text |
187
|
|
|
*/ |
188
|
|
|
public function parseCaseSensitive($source) |
189
|
|
|
{ |
190
|
|
|
return $this->parse($source, false); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Helper function to parse case insensitive |
195
|
|
|
* @param string $source String containing the BBCode |
196
|
|
|
* @return string Parsed text |
197
|
|
|
*/ |
198
|
|
|
public function parseCaseInsensitive($source) |
199
|
|
|
{ |
200
|
|
|
return $this->parse($source, true); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Limits the parsers to only those you specify |
205
|
|
|
* @param mixed $only parsers |
206
|
|
|
* @return object BBCodeParser object |
207
|
|
|
*/ |
208
|
|
|
public function only($only = null) |
209
|
|
|
{ |
210
|
|
|
$only = (is_array($only)) ? $only : func_get_args(); |
211
|
|
|
$this->enabledParsers = $this->arrayOnly($this->parsers, $only); |
212
|
|
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Removes the parsers you want to exclude |
217
|
|
|
* @param mixed $except parsers |
218
|
|
|
* @return object BBCodeParser object |
219
|
|
|
*/ |
220
|
|
|
public function except($except = null) |
221
|
|
|
{ |
222
|
|
|
$except = (is_array($except)) ? $except : func_get_args(); |
223
|
|
|
$this->enabledParsers = $this->arrayExcept($this->parsers, $except); |
224
|
|
|
return $this; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* List of chosen parsers |
229
|
|
|
* @return array array of parsers |
230
|
|
|
*/ |
231
|
|
|
public function getParsers() |
232
|
|
|
{ |
233
|
|
|
return $this->enabledParsers; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Sets the parser pattern and replace. |
238
|
|
|
* This can be used for new parsers or overwriting existing ones. |
239
|
|
|
* @param string $name Parser name |
240
|
|
|
* @param string $pattern Pattern |
241
|
|
|
* @param string $replace Replace pattern |
242
|
|
|
* @param string $content Parsed text pattern |
243
|
|
|
* @return void |
244
|
|
|
*/ |
245
|
|
|
public function setParser($name, $pattern, $replace, $content) |
246
|
|
|
{ |
247
|
|
|
$this->parsers[$name] = array( |
248
|
|
|
'pattern' => $pattern, |
249
|
|
|
'replace' => $replace, |
250
|
|
|
'content' => $content |
251
|
|
|
); |
252
|
|
|
|
253
|
|
|
$this->enabledParsers[$name] = array( |
254
|
|
|
'pattern' => $pattern, |
255
|
|
|
'replace' => $replace, |
256
|
|
|
'content' => $content |
257
|
|
|
); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|