]> git.mxchange.org Git - friendica.git/blob - include/bbcode.php
Merge branch 'master' of https://github.com/friendica/friendica into threaded_items
[friendica.git] / include / bbcode.php
1 <?php
2
3 require_once("include/oembed.php");
4 require_once('include/event.php');
5
6
7
8 function stripcode_br_cb($s) {
9         return '[code]' . str_replace('<br />', '', $s[1]) . '[/code]';
10 }
11
12 function tryoembed($match){
13         $url = ((count($match)==2)?$match[1]:$match[2]);
14 //      logger("tryoembed: $url");
15
16         $o = oembed_fetch_url($url);
17
18         //echo "<pre>"; var_dump($match, $url, $o); killme();
19
20         if ($o->type=="error") return $match[0];
21
22         $html = oembed_format_object($o);
23         return $html; //oembed_iframe($html,$o->width,$o->height);
24
25 }
26
27 // [noparse][i]italic[/i][/noparse] turns into
28 // [noparse][ i ]italic[ /i ][/noparse],
29 // to hide them from parser.
30
31 function bb_spacefy($st) {
32   $whole_match = $st[0];
33   $captured = $st[1];
34   $spacefied = preg_replace("/\[(.*?)\]/", "[ $1 ]", $captured);
35   $new_str = str_replace($captured, $spacefied, $whole_match);
36   return $new_str;
37 }
38
39 // The previously spacefied [noparse][ i ]italic[ /i ][/noparse],
40 // now turns back and the [noparse] tags are trimed
41 // returning [i]italic[/i]
42
43 function bb_unspacefy_and_trim($st) {
44   $whole_match = $st[0];
45   $captured = $st[1];
46   $unspacefied = preg_replace("/\[ (.*?)\ ]/", "[$1]", $captured);
47   return $unspacefied;
48 }
49
50 if(! function_exists('bb_extract_images')) {
51 function bb_extract_images($body) {
52
53         $saved_image = array();
54         $orig_body = $body;
55         $new_body = '';
56
57         $cnt = 0;
58         $img_start = strpos($orig_body, '[img');
59         $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
60         $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
61         while(($img_st_close !== false) && ($img_end !== false)) {
62
63                 $img_st_close++; // make it point to AFTER the closing bracket
64                 $img_end += $img_start;
65
66                 if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
67                         // This is an embedded image
68
69                         $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
70                         $new_body = $new_body . substr($orig_body, 0, $img_start) . '[$#saved_image' . $cnt . '#$]';
71
72                         $cnt++;
73                 }
74                 else
75                         $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
76
77                 $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
78
79                 if($orig_body === false) // in case the body ends on a closing image tag
80                         $orig_body = '';
81
82                 $img_start = strpos($orig_body, '[img');
83                 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
84                 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
85         }
86
87         $new_body = $new_body . $orig_body;
88
89         return array('body' => $new_body, 'images' => $saved_image);
90 }}
91
92 if(! function_exists('bb_replace_images')) {
93 function bb_replace_images($body, $images) {
94
95         $newbody = $body;
96
97         $cnt = 0;
98         foreach($images as $image) {
99                 // We're depending on the property of 'foreach' (specified on the PHP website) that
100                 // it loops over the array starting from the first element and going sequentially
101                 // to the last element
102                 $newbody = str_replace('[$#saved_image' . $cnt . '#$]', '<img src="' . $image .'" alt="' . t('Image/photo') . '" />', $newbody);
103                 $cnt++;
104         }
105
106         return $newbody;
107 }}
108
109
110
111         // BBcode 2 HTML was written by WAY2WEB.net
112         // extended to work with Mistpark/Friendica - Mike Macgirvin
113
114 function bbcode($Text,$preserve_nl = false, $tryoembed = true) {
115
116         $a = get_app();
117
118         // Move all spaces out of the tags
119         $Text = preg_replace("/\[(\w*)\](\s*)/ism", '$2[$1]', $Text);
120         $Text = preg_replace("/(\s*)\[\/(\w*)\]/ism", '[/$2]$1', $Text);
121
122         // Hide all [noparse] contained bbtags by spacefying them
123         // POSSIBLE BUG --> Will the 'preg' functions crash if there's an embedded image?
124
125         $Text = preg_replace_callback("/\[noparse\](.*?)\[\/noparse\]/ism", 'bb_spacefy',$Text);
126         $Text = preg_replace_callback("/\[nobb\](.*?)\[\/nobb\]/ism", 'bb_spacefy',$Text);
127         $Text = preg_replace_callback("/\[pre\](.*?)\[\/pre\]/ism", 'bb_spacefy',$Text);
128
129
130         // Extract the private images which use data url's since preg has issues with
131         // large data sizes. Stash them away while we do bbcode conversion, and then put them back
132         // in after we've done all the regex matching. We cannot use any preg functions to do this.
133
134         $extracted = bb_extract_images($Text);
135         $Text = $extracted['body'];
136         $saved_image = $extracted['images'];
137
138         // If we find any event code, turn it into an event.
139         // After we're finished processing the bbcode we'll
140         // replace all of the event code with a reformatted version.
141
142         $ev = bbtoevent($Text);
143
144
145         // Replace any html brackets with HTML Entities to prevent executing HTML or script
146         // Don't use strip_tags here because it breaks [url] search by replacing & with amp
147
148         $Text = str_replace("<", "&lt;", $Text);
149         $Text = str_replace(">", "&gt;", $Text);
150
151         // Convert new line chars to html <br /> tags
152
153         // nlbr seems to be hopelessly messed up
154         //      $Text = nl2br($Text);
155
156         // We'll emulate it.
157
158         $Text = str_replace("\r\n","\n", $Text);
159         $Text = str_replace(array("\r","\n"), array('<br />','<br />'), $Text);
160
161         if($preserve_nl)
162                 $Text = str_replace(array("\n","\r"), array('',''),$Text);
163
164
165
166         // Set up the parameters for a URL search string
167         $URLSearchString = "^\[\]";
168         // Set up the parameters for a MAIL search string
169         $MAILSearchString = $URLSearchString;
170
171
172         // Perform URL Search
173
174         $Text = preg_replace("/([^\]\=]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1<a href="$2" target="external-link">$2</a>', $Text);
175
176         if ($tryoembed)
177                 $Text = preg_replace_callback("/\[bookmark\=([^\]]*)\].*?\[\/bookmark\]/ism",'tryoembed',$Text);
178
179         $Text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",'[url=$1]$2[/url]',$Text);
180
181         if ($tryoembed)
182                 $Text = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism",'tryoembed',$Text);
183
184         $Text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/ism", '<a href="$1" target="external-link">$1</a>', $Text);
185         $Text = preg_replace("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '<a href="$1" target="external-link">$2</a>', $Text);
186         //$Text = preg_replace("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", '<a href="$1" target="_blank">$2</a>', $Text);
187
188         // we may need to restrict this further if it picks up too many strays
189         // link acct:user@host to a webfinger profile redirector
190
191         $Text = preg_replace('/acct:(.*?)@(.*?)([ ,])/', '<a href="' . $a->get_baseurl() . '/acctlink?addr=' . "$1@$2" 
192                 . '" target="extlink" >acct:' . "$1@$2$3" . '</a>',$Text);
193
194         // Perform MAIL Search
195         $Text = preg_replace("/\[mail\]([$MAILSearchString]*)\[\/mail\]/", '<a href="mailto:$1">$1</a>', $Text);
196         $Text = preg_replace("/\[mail\=([$MAILSearchString]*)\](.*?)\[\/mail\]/", '<a href="mailto:$1">$2</a>', $Text);
197
198         // Check for bold text
199         $Text = preg_replace("(\[b\](.*?)\[\/b\])ism",'<strong>$1</strong>',$Text);
200
201         // Check for Italics text
202         $Text = preg_replace("(\[i\](.*?)\[\/i\])ism",'<em>$1</em>',$Text);
203
204         // Check for Underline text
205         $Text = preg_replace("(\[u\](.*?)\[\/u\])ism",'<u>$1</u>',$Text);
206
207         // Check for strike-through text
208         $Text = preg_replace("(\[s\](.*?)\[\/s\])ism",'<strike>$1</strike>',$Text);
209
210         // Check for over-line text
211         $Text = preg_replace("(\[o\](.*?)\[\/o\])ism",'<span class="overline">$1</span>',$Text);
212
213         // Check for colored text
214         $Text = preg_replace("(\[color=(.*?)\](.*?)\[\/color\])ism","<span style=\"color: $1;\">$2</span>",$Text);
215
216         // Check for sized text
217         // [size=50] --> font-size: 50px (with the unit).
218         $Text = preg_replace("(\[size=(\d*?)\](.*?)\[\/size\])ism","<span style=\"font-size: $1px;\">$2</span>",$Text);
219         $Text = preg_replace("(\[size=(.*?)\](.*?)\[\/size\])ism","<span style=\"font-size: $1;\">$2</span>",$Text);
220
221         // Check for centered text
222         $Text = preg_replace("(\[center\](.*?)\[\/center\])ism","<div style=\"text-align:center;\">$1</div>",$Text);
223
224         // Check for list text
225         $Text = str_replace("[*]", "<li>", $Text);
226
227         // Check for style sheet commands
228         $Text = preg_replace("(\[style=(.*?)\](.*?)\[\/style\])ism","<span style=\"$1;\">$2</span>",$Text);
229
230         // Check for CSS classes
231         $Text = preg_replace("(\[class=(.*?)\](.*?)\[\/class\])ism","<span class=\"$1\">$2</span>",$Text);
232
233         // handle nested lists
234         $endlessloop = 0;
235
236         while ((((strpos($Text, "[/list]") !== false) && (strpos($Text, "[list") !== false)) ||
237                ((strpos($Text, "[/ol]") !== false) && (strpos($Text, "[ol]") !== false)) || 
238                ((strpos($Text, "[/ul]") !== false) && (strpos($Text, "[ul]") !== false)) || 
239                ((strpos($Text, "[/li]") !== false) && (strpos($Text, "[li]") !== false))) && (++$endlessloop < 20)) {
240                 $Text = preg_replace("/\[list\](.*?)\[\/list\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>' ,$Text);
241                 $Text = preg_replace("/\[list=\](.*?)\[\/list\]/ism", '<ul class="listnone" style="list-style-type: none;">$1</ul>' ,$Text);
242                 $Text = preg_replace("/\[list=1\](.*?)\[\/list\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>' ,$Text);
243                 $Text = preg_replace("/\[list=((?-i)i)\](.*?)\[\/list\]/ism",'<ul class="listlowerroman" style="list-style-type: lower-roman;">$2</ul>' ,$Text);
244                 $Text = preg_replace("/\[list=((?-i)I)\](.*?)\[\/list\]/ism", '<ul class="listupperroman" style="list-style-type: upper-roman;">$2</ul>' ,$Text);
245                 $Text = preg_replace("/\[list=((?-i)a)\](.*?)\[\/list\]/ism", '<ul class="listloweralpha" style="list-style-type: lower-alpha;">$2</ul>' ,$Text);
246                 $Text = preg_replace("/\[list=((?-i)A)\](.*?)\[\/list\]/ism", '<ul class="listupperalpha" style="list-style-type: upper-alpha;">$2</ul>' ,$Text);
247                 $Text = preg_replace("/\[ul\](.*?)\[\/ul\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>' ,$Text);
248                 $Text = preg_replace("/\[ol\](.*?)\[\/ol\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>' ,$Text);
249                 $Text = preg_replace("/\[li\](.*?)\[\/li\]/ism", '<li>$1</li>' ,$Text);
250         }
251
252         $Text = preg_replace("/\[th\](.*?)\[\/th\]/sm", '<th>$1</th>' ,$Text);
253         $Text = preg_replace("/\[td\](.*?)\[\/td\]/sm", '<td>$1</td>' ,$Text);
254         $Text = preg_replace("/\[tr\](.*?)\[\/tr\]/sm", '<tr>$1</tr>' ,$Text);
255         $Text = preg_replace("/\[table\](.*?)\[\/table\]/sm", '<table>$1</table>' ,$Text);
256
257         $Text = preg_replace("/\[table border=1\](.*?)\[\/table\]/sm", '<table border="1" >$1</table>' ,$Text);
258         $Text = preg_replace("/\[table border=0\](.*?)\[\/table\]/sm", '<table border="0" >$1</table>' ,$Text);
259
260         $Text = str_replace('[hr]','<hr />', $Text);
261
262         // This is actually executed in prepare_body()
263
264         $Text = str_replace('[nosmile]','',$Text);
265
266         // Check for font change text
267         $Text = preg_replace("/\[font=(.*?)\](.*?)\[\/font\]/sm","<span style=\"font-family: $1;\">$2</span>",$Text);
268
269         // Declare the format for [code] layout
270
271 //      $Text = preg_replace_callback("/\[code\](.*?)\[\/code\]/ism",'stripcode_br_cb',$Text);
272
273         $CodeLayout = '<code>$1</code>';
274         // Check for [code] text
275         $Text = preg_replace("/\[code\](.*?)\[\/code\]/ism","$CodeLayout", $Text);
276
277         // Declare the format for [spoiler] layout
278         $SpoilerLayout = '<blockquote class="spoiler">$1</blockquote>';
279
280         // Check for [spoiler] text
281         // handle nested quotes
282         $endlessloop = 0;
283         while ((strpos($Text, "[/spoiler]") !== false) and (strpos($Text, "[spoiler]") !== false) and (++$endlessloop < 20))
284                 $Text = preg_replace("/\[spoiler\](.*?)\[\/spoiler\]/ism","$SpoilerLayout", $Text);
285
286         // Check for [spoiler=Author] text
287
288         $t_wrote = t('$1 wrote:');
289
290         // handle nested quotes
291         $endlessloop = 0;
292         while ((strpos($Text, "[/spoiler]")!== false)  and (strpos($Text, "[spoiler=") !== false) and (++$endlessloop < 20))
293                 $Text = preg_replace("/\[spoiler=[\"\']*(.*?)[\"\']*\](.*?)\[\/spoiler\]/ism",
294                                      "<br /><strong class=".'"spoiler"'.">" . $t_wrote . "</strong><blockquote class=".'"spoiler"'.">$2</blockquote>",
295                                      $Text);
296
297         // Declare the format for [quote] layout
298         $QuoteLayout = '<blockquote>$1</blockquote>';
299
300         // Check for [quote] text
301         // handle nested quotes
302         $endlessloop = 0;
303         while ((strpos($Text, "[/quote]") !== false) and (strpos($Text, "[quote]") !== false) and (++$endlessloop < 20))
304                 $Text = preg_replace("/\[quote\](.*?)\[\/quote\]/ism","$QuoteLayout", $Text);
305
306         // Check for [quote=Author] text
307
308         $t_wrote = t('$1 wrote:');
309
310         // handle nested quotes
311         $endlessloop = 0;
312         while ((strpos($Text, "[/quote]")!== false)  and (strpos($Text, "[quote=") !== false) and (++$endlessloop < 20))
313                 $Text = preg_replace("/\[quote=[\"\']*(.*?)[\"\']*\](.*?)\[\/quote\]/ism",
314                                      "<br /><strong class=".'"author"'.">" . $t_wrote . "</strong><blockquote>$2</blockquote>",
315                                      $Text);
316
317         // [img=widthxheight]image source[/img]
318         //$Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '<img src="$3" style="height: $2px; width: $1px;" >', $Text);
319         $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '<img src="$3" style="width: $1px;" >', $Text);
320
321         // Images
322         // [img]pathtoimage[/img]
323         $Text = preg_replace("/\[img\](.*?)\[\/img\]/ism", '<img src="$1" alt="' . t('Image/photo') . '" />', $Text);
324
325
326         // Try to Oembed
327         if ($tryoembed) {
328                 $Text = preg_replace("/\[video\](.*?\.(ogg|ogv|oga|ogm|webm|mp4))\[\/video\]/ism", '<video src="$1" controls="controls" width="425" height="350"><a href="$1">$1</a></video>', $Text);
329                 $Text = preg_replace("/\[audio\](.*?\.(ogg|ogv|oga|ogm|webm|mp4|mp3))\[\/audio\]/ism", '<audio src="$1" controls="controls"><a href="$1">$1</a></audio>', $Text);
330
331                 $Text = preg_replace_callback("/\[video\](.*?)\[\/video\]/ism", 'tryoembed', $Text);
332                 $Text = preg_replace_callback("/\[audio\](.*?)\[\/audio\]/ism", 'tryoembed', $Text);
333         } else {
334                 $Text = preg_replace("/\[video\](.*?)\[\/video\]/", '$1', $Text);
335                 $Text = preg_replace("/\[audio\](.*?)\[\/audio\]/", '$1', $Text);
336         }
337
338         // html5 video and audio
339
340
341         if ($tryoembed)
342                 $Text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/ism", '<iframe src="$1" width="425" height="350"><a href="$1">$1</a></iframe>', $Text);
343         else
344                 $Text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/ism", '<a href="$1">$1</a>', $Text);
345
346         // Youtube extensions
347         if ($tryoembed) {
348                 $Text = preg_replace_callback("/\[youtube\](https?:\/\/www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", 'tryoembed', $Text);        
349                 $Text = preg_replace_callback("/\[youtube\](www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", 'tryoembed', $Text);        
350                 $Text = preg_replace_callback("/\[youtube\](https?:\/\/youtu.be\/.*?)\[\/youtube\]/ism",'tryoembed',$Text); 
351         }
352
353         $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text); 
354         $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/embed\/(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text); 
355         $Text = preg_replace("/\[youtube\]https?:\/\/youtu.be\/(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text);
356
357         if ($tryoembed)
358                 $Text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism", '<iframe width="425" height="350" src="http://www.youtube.com/embed/$1" frameborder="0" ></iframe>', $Text);
359         else
360                 $Text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism", "http://www.youtube.com/watch?v=$1", $Text);
361
362
363         if ($tryoembed) {
364                 $Text = preg_replace_callback("/\[vimeo\](https?:\/\/player.vimeo.com\/video\/[0-9]+).*?\[\/vimeo\]/ism",'tryoembed',$Text); 
365                 $Text = preg_replace_callback("/\[vimeo\](https?:\/\/vimeo.com\/[0-9]+).*?\[\/vimeo\]/ism",'tryoembed',$Text); 
366         }
367
368         $Text = preg_replace("/\[vimeo\]https?:\/\/player.vimeo.com\/video\/([0-9]+)(.*?)\[\/vimeo\]/ism",'[vimeo]$1[/vimeo]',$Text); 
369         $Text = preg_replace("/\[vimeo\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/vimeo\]/ism",'[vimeo]$1[/vimeo]',$Text);
370
371         if ($tryoembed)
372                 $Text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism", '<iframe width="425" height="350" src="http://player.vimeo.com/video/$1" frameborder="0" ></iframe>', $Text);
373         else
374                 $Text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism", "http://vimeo.com/$1", $Text);
375
376 //      $Text = preg_replace("/\[youtube\](.*?)\[\/youtube\]/", '<object width="425" height="350" type="application/x-shockwave-flash" data="http://www.youtube.com/v/$1" ><param name="movie" value="http://www.youtube.com/v/$1"></param><!--[if IE]><embed src="http://www.youtube.com/v/$1" type="application/x-shockwave-flash" width="425" height="350" /><![endif]--></object>', $Text);
377
378
379         // oembed tag
380         $Text = oembed_bbcode2html($Text);
381
382         // Avoid triple linefeeds through oembed
383         $Text = str_replace("<br style='clear:left'></span><br /><br />", "<br style='clear:left'></span><br />", $Text);
384
385         // If we found an event earlier, strip out all the event code and replace with a reformatted version.
386         // Replace the event-start section with the entire formatted event. The other bbcode is stripped.
387         // Summary (e.g. title) is required, earlier revisions only required description (in addition to 
388         // start which is always required). Allow desc with a missing summary for compatibility.
389
390         if((x($ev,'desc') || x($ev,'summary')) && x($ev,'start')) {
391                 $sub = format_event_html($ev);
392
393                 $Text = preg_replace("/\[event\-summary\](.*?)\[\/event\-summary\]/ism",'',$Text);
394                 $Text = preg_replace("/\[event\-description\](.*?)\[\/event\-description\]/ism",'',$Text);
395                 $Text = preg_replace("/\[event\-start\](.*?)\[\/event\-start\]/ism",$sub,$Text); 
396                 $Text = preg_replace("/\[event\-finish\](.*?)\[\/event\-finish\]/ism",'',$Text);
397                 $Text = preg_replace("/\[event\-location\](.*?)\[\/event\-location\]/ism",'',$Text);
398                 $Text = preg_replace("/\[event\-adjust\](.*?)\[\/event\-adjust\]/ism",'',$Text);
399         }
400
401         // Unhide all [noparse] contained bbtags unspacefying them 
402         // and triming the [noparse] tag.
403
404         $Text = preg_replace_callback("/\[noparse\](.*?)\[\/noparse\]/ism", 'bb_unspacefy_and_trim',$Text);
405         $Text = preg_replace_callback("/\[nobb\](.*?)\[\/nobb\]/ism", 'bb_unspacefy_and_trim',$Text);
406         $Text = preg_replace_callback("/\[pre\](.*?)\[\/pre\]/ism", 'bb_unspacefy_and_trim',$Text);
407
408
409         $Text = preg_replace('/\[\&amp\;([#a-z0-9]+)\;\]/','&$1;',$Text);
410
411         // fix any escaped ampersands that may have been converted into links
412         $Text = preg_replace("/\<(.*?)(src|href)=(.*?)\&amp\;(.*?)\>/ism",'<$1$2=$3&$4>',$Text);
413         $Text = preg_replace("/\<(.*?)(src|href)=\"[^hfm](.*?)\>/ism",'<$1$2="">',$Text);
414
415         if($saved_image)
416                 $Text = bb_replace_images($Text, $saved_image);
417
418         // Clean up the HTML by loading and saving the HTML with the DOM
419         // Only do it when it has to be done - for performance reasons
420         if (!$tryoembed) {
421                 $doc = new DOMDocument();
422                 $doc->preserveWhiteSpace = false;
423
424                 $Text = mb_convert_encoding($Text, 'HTML-ENTITIES', "UTF-8");
425
426                 $doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">';
427                 @$doc->loadHTML($doctype."<html><body>".$Text."</body></html>");
428
429                 $Text = $doc->saveHTML();
430                 $Text = str_replace(array("<html><body>", "</body></html>", $doctype), array("", "", ""), $Text);
431
432                 $Text = str_replace('<br></li>','</li>', $Text);
433
434                 $Text = mb_convert_encoding($Text, "UTF-8", 'HTML-ENTITIES');
435         }
436
437         call_hooks('bbcode',$Text);
438
439         return $Text;
440 }
441