3 require_once("include/oembed.php");
4 require_once('include/event.php');
8 function stripcode_br_cb($s) {
9 return '[code]' . str_replace('<br />', '', $s[1]) . '[/code]';
12 function tryoembed($match){
13 $url = ((count($match)==2)?$match[1]:$match[2]);
14 // logger("tryoembed: $url");
16 $o = oembed_fetch_url($url);
18 //echo "<pre>"; var_dump($match, $url, $o); killme();
20 if ($o->type=="error") return $match[0];
22 $html = oembed_format_object($o);
23 return $html; //oembed_iframe($html,$o->width,$o->height);
27 // [noparse][i]italic[/i][/noparse] turns into
28 // [noparse][ i ]italic[ /i ][/noparse],
29 // to hide them from parser.
31 function bb_spacefy($st) {
32 $whole_match = $st[0];
34 $spacefied = preg_replace("/\[(.*?)\]/", "[ $1 ]", $captured);
35 $new_str = str_replace($captured, $spacefied, $whole_match);
39 // The previously spacefied [noparse][ i ]italic[ /i ][/noparse],
40 // now turns back and the [noparse] tags are trimed
41 // returning [i]italic[/i]
43 function bb_unspacefy_and_trim($st) {
44 $whole_match = $st[0];
46 $unspacefied = preg_replace("/\[ (.*?)\ ]/", "[$1]", $captured);
50 function get_bb_tag_pos($s, $name, $occurance = 1) {
56 for($i = 1; $i <= $occurance; $i++) {
57 if( $start_open !== false)
58 $start_open = strpos($s, '[' . $name, $start_open + 1); // allow [name= type tags
61 if( $start_open === false)
64 $start_equal = strpos($s, '=', $start_open);
65 $start_close = strpos($s, ']', $start_open);
67 if( $start_close === false)
72 $end_open = strpos($s, '[/' . $name . ']', $start_close);
74 if( $end_open === false)
77 $res = array( 'start' => array('open' => $start_open, 'close' => $start_close),
78 'end' => array('open' => $end_open, 'close' => $end_open + strlen('[/' . $name . ']')) );
79 if( $start_equal !== false)
80 $res['start']['equal'] = $start_equal + 1;
85 function bb_tag_preg_replace($pattern, $replace, $name, $s) {
90 $pos = get_bb_tag_pos($string, $name, $occurance);
91 while($pos !== false && $occurance < 1000) {
93 $start = substr($string, 0, $pos['start']['open']);
94 $subject = substr($string, $pos['start']['open'], $pos['end']['close'] - $pos['start']['open']);
95 $end = substr($string, $pos['end']['close']);
99 $subject = preg_replace($pattern, $replace, $subject);
100 $string = $start . $subject . $end;
103 $pos = get_bb_tag_pos($string, $name, $occurance);
109 if(! function_exists('bb_extract_images')) {
110 function bb_extract_images($body) {
112 $saved_image = array();
117 $img_start = strpos($orig_body, '[img');
118 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
119 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
120 while(($img_st_close !== false) && ($img_end !== false)) {
122 $img_st_close++; // make it point to AFTER the closing bracket
123 $img_end += $img_start;
125 if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
126 // This is an embedded image
128 $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
129 $new_body = $new_body . substr($orig_body, 0, $img_start) . '[$#saved_image' . $cnt . '#$]';
134 $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
136 $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
138 if($orig_body === false) // in case the body ends on a closing image tag
141 $img_start = strpos($orig_body, '[img');
142 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
143 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
146 $new_body = $new_body . $orig_body;
148 return array('body' => $new_body, 'images' => $saved_image);
151 if(! function_exists('bb_replace_images')) {
152 function bb_replace_images($body, $images) {
157 foreach($images as $image) {
158 // We're depending on the property of 'foreach' (specified on the PHP website) that
159 // it loops over the array starting from the first element and going sequentially
160 // to the last element
161 $newbody = str_replace('[$#saved_image' . $cnt . '#$]', '<img src="' . $image .'" alt="' . t('Image/photo') . '" />', $newbody);
170 // BBcode 2 HTML was written by WAY2WEB.net
171 // extended to work with Mistpark/Friendica - Mike Macgirvin
173 function bbcode($Text,$preserve_nl = false, $tryoembed = true) {
177 // Move all spaces out of the tags
178 $Text = preg_replace("/\[(\w*)\](\s*)/ism", '$2[$1]', $Text);
179 $Text = preg_replace("/(\s*)\[\/(\w*)\]/ism", '[/$2]$1', $Text);
181 // Hide all [noparse] contained bbtags by spacefying them
182 // POSSIBLE BUG --> Will the 'preg' functions crash if there's an embedded image?
184 $Text = preg_replace_callback("/\[noparse\](.*?)\[\/noparse\]/ism", 'bb_spacefy',$Text);
185 $Text = preg_replace_callback("/\[nobb\](.*?)\[\/nobb\]/ism", 'bb_spacefy',$Text);
186 $Text = preg_replace_callback("/\[pre\](.*?)\[\/pre\]/ism", 'bb_spacefy',$Text);
189 // Extract the private images which use data url's since preg has issues with
190 // large data sizes. Stash them away while we do bbcode conversion, and then put them back
191 // in after we've done all the regex matching. We cannot use any preg functions to do this.
193 $extracted = bb_extract_images($Text);
194 $Text = $extracted['body'];
195 $saved_image = $extracted['images'];
197 // If we find any event code, turn it into an event.
198 // After we're finished processing the bbcode we'll
199 // replace all of the event code with a reformatted version.
201 $ev = bbtoevent($Text);
204 // Replace any html brackets with HTML Entities to prevent executing HTML or script
205 // Don't use strip_tags here because it breaks [url] search by replacing & with amp
207 $Text = str_replace("<", "<", $Text);
208 $Text = str_replace(">", ">", $Text);
210 // Convert new line chars to html <br /> tags
212 // nlbr seems to be hopelessly messed up
213 // $Text = nl2br($Text);
217 $Text = str_replace("\r\n","\n", $Text);
218 $Text = str_replace(array("\r","\n"), array('<br />','<br />'), $Text);
221 $Text = str_replace(array("\n","\r"), array('',''),$Text);
225 // Set up the parameters for a URL search string
226 $URLSearchString = "^\[\]";
227 // Set up the parameters for a MAIL search string
228 $MAILSearchString = $URLSearchString;
231 // Perform URL Search
233 $Text = preg_replace("/([^\]\=]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1<a href="$2" target="external-link">$2</a>', $Text);
236 $Text = preg_replace_callback("/\[bookmark\=([^\]]*)\].*?\[\/bookmark\]/ism",'tryoembed',$Text);
238 $Text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",'[url=$1]$2[/url]',$Text);
241 $Text = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism",'tryoembed',$Text);
243 $Text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/ism", '<a href="$1" target="external-link">$1</a>', $Text);
244 $Text = preg_replace("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '<a href="$1" target="external-link">$2</a>', $Text);
245 //$Text = preg_replace("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", '<a href="$1" target="_blank">$2</a>', $Text);
247 // we may need to restrict this further if it picks up too many strays
248 // link acct:user@host to a webfinger profile redirector
250 $Text = preg_replace('/acct:(.*?)@(.*?)([ ,])/', '<a href="' . $a->get_baseurl() . '/acctlink?addr=' . "$1@$2"
251 . '" target="extlink" >acct:' . "$1@$2$3" . '</a>',$Text);
253 // Perform MAIL Search
254 $Text = preg_replace("/\[mail\]([$MAILSearchString]*)\[\/mail\]/", '<a href="mailto:$1">$1</a>', $Text);
255 $Text = preg_replace("/\[mail\=([$MAILSearchString]*)\](.*?)\[\/mail\]/", '<a href="mailto:$1">$2</a>', $Text);
257 // Check for bold text
258 $Text = preg_replace("(\[b\](.*?)\[\/b\])ism",'<strong>$1</strong>',$Text);
260 // Check for Italics text
261 $Text = preg_replace("(\[i\](.*?)\[\/i\])ism",'<em>$1</em>',$Text);
263 // Check for Underline text
264 $Text = preg_replace("(\[u\](.*?)\[\/u\])ism",'<u>$1</u>',$Text);
266 // Check for strike-through text
267 $Text = preg_replace("(\[s\](.*?)\[\/s\])ism",'<strike>$1</strike>',$Text);
269 // Check for over-line text
270 $Text = preg_replace("(\[o\](.*?)\[\/o\])ism",'<span class="overline">$1</span>',$Text);
272 // Check for colored text
273 $Text = preg_replace("(\[color=(.*?)\](.*?)\[\/color\])ism","<span style=\"color: $1;\">$2</span>",$Text);
275 // Check for sized text
276 // [size=50] --> font-size: 50px (with the unit).
277 $Text = preg_replace("(\[size=(\d*?)\](.*?)\[\/size\])ism","<span style=\"font-size: $1px;\">$2</span>",$Text);
278 $Text = preg_replace("(\[size=(.*?)\](.*?)\[\/size\])ism","<span style=\"font-size: $1;\">$2</span>",$Text);
280 // Check for centered text
281 $Text = preg_replace("(\[center\](.*?)\[\/center\])ism","<div style=\"text-align:center;\">$1</div>",$Text);
283 // Check for list text
284 $Text = str_replace("[*]", "<li>", $Text);
286 // Check for style sheet commands
287 $Text = preg_replace("(\[style=(.*?)\](.*?)\[\/style\])ism","<span style=\"$1;\">$2</span>",$Text);
289 // Check for CSS classes
290 $Text = preg_replace("(\[class=(.*?)\](.*?)\[\/class\])ism","<span class=\"$1\">$2</span>",$Text);
292 // handle nested lists
295 while ((((strpos($Text, "[/list]") !== false) && (strpos($Text, "[list") !== false)) ||
296 ((strpos($Text, "[/ol]") !== false) && (strpos($Text, "[ol]") !== false)) ||
297 ((strpos($Text, "[/ul]") !== false) && (strpos($Text, "[ul]") !== false)) ||
298 ((strpos($Text, "[/li]") !== false) && (strpos($Text, "[li]") !== false))) && (++$endlessloop < 20)) {
299 $Text = preg_replace("/\[list\](.*?)\[\/list\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>' ,$Text);
300 $Text = preg_replace("/\[list=\](.*?)\[\/list\]/ism", '<ul class="listnone" style="list-style-type: none;">$1</ul>' ,$Text);
301 $Text = preg_replace("/\[list=1\](.*?)\[\/list\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>' ,$Text);
302 $Text = preg_replace("/\[list=((?-i)i)\](.*?)\[\/list\]/ism",'<ul class="listlowerroman" style="list-style-type: lower-roman;">$2</ul>' ,$Text);
303 $Text = preg_replace("/\[list=((?-i)I)\](.*?)\[\/list\]/ism", '<ul class="listupperroman" style="list-style-type: upper-roman;">$2</ul>' ,$Text);
304 $Text = preg_replace("/\[list=((?-i)a)\](.*?)\[\/list\]/ism", '<ul class="listloweralpha" style="list-style-type: lower-alpha;">$2</ul>' ,$Text);
305 $Text = preg_replace("/\[list=((?-i)A)\](.*?)\[\/list\]/ism", '<ul class="listupperalpha" style="list-style-type: upper-alpha;">$2</ul>' ,$Text);
306 $Text = preg_replace("/\[ul\](.*?)\[\/ul\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>' ,$Text);
307 $Text = preg_replace("/\[ol\](.*?)\[\/ol\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>' ,$Text);
308 $Text = preg_replace("/\[li\](.*?)\[\/li\]/ism", '<li>$1</li>' ,$Text);
311 $Text = preg_replace("/\[th\](.*?)\[\/th\]/sm", '<th>$1</th>' ,$Text);
312 $Text = preg_replace("/\[td\](.*?)\[\/td\]/sm", '<td>$1</td>' ,$Text);
313 $Text = preg_replace("/\[tr\](.*?)\[\/tr\]/sm", '<tr>$1</tr>' ,$Text);
314 $Text = preg_replace("/\[table\](.*?)\[\/table\]/sm", '<table>$1</table>' ,$Text);
316 $Text = preg_replace("/\[table border=1\](.*?)\[\/table\]/sm", '<table border="1" >$1</table>' ,$Text);
317 $Text = preg_replace("/\[table border=0\](.*?)\[\/table\]/sm", '<table border="0" >$1</table>' ,$Text);
319 $Text = str_replace('[hr]','<hr />', $Text);
321 // This is actually executed in prepare_body()
323 $Text = str_replace('[nosmile]','',$Text);
325 // Check for font change text
326 $Text = preg_replace("/\[font=(.*?)\](.*?)\[\/font\]/sm","<span style=\"font-family: $1;\">$2</span>",$Text);
328 // Declare the format for [code] layout
330 // $Text = preg_replace_callback("/\[code\](.*?)\[\/code\]/ism",'stripcode_br_cb',$Text);
332 $CodeLayout = '<code>$1</code>';
333 // Check for [code] text
334 $Text = preg_replace("/\[code\](.*?)\[\/code\]/ism","$CodeLayout", $Text);
336 // Declare the format for [spoiler] layout
337 $SpoilerLayout = '<blockquote class="spoiler">$1</blockquote>';
339 // Check for [spoiler] text
340 // handle nested quotes
342 while ((strpos($Text, "[/spoiler]") !== false) and (strpos($Text, "[spoiler]") !== false) and (++$endlessloop < 20))
343 $Text = preg_replace("/\[spoiler\](.*?)\[\/spoiler\]/ism","$SpoilerLayout", $Text);
345 // Check for [spoiler=Author] text
347 $t_wrote = t('$1 wrote:');
349 // handle nested quotes
351 while ((strpos($Text, "[/spoiler]")!== false) and (strpos($Text, "[spoiler=") !== false) and (++$endlessloop < 20))
352 $Text = preg_replace("/\[spoiler=[\"\']*(.*?)[\"\']*\](.*?)\[\/spoiler\]/ism",
353 "<br /><strong class=".'"spoiler"'.">" . $t_wrote . "</strong><blockquote class=".'"spoiler"'.">$2</blockquote>",
356 // Declare the format for [quote] layout
357 $QuoteLayout = '<blockquote>$1</blockquote>';
359 // Check for [quote] text
360 // handle nested quotes
362 while ((strpos($Text, "[/quote]") !== false) and (strpos($Text, "[quote]") !== false) and (++$endlessloop < 20))
363 $Text = preg_replace("/\[quote\](.*?)\[\/quote\]/ism","$QuoteLayout", $Text);
365 // Check for [quote=Author] text
367 $t_wrote = t('$1 wrote:');
369 // handle nested quotes
371 while ((strpos($Text, "[/quote]")!== false) and (strpos($Text, "[quote=") !== false) and (++$endlessloop < 20))
372 $Text = preg_replace("/\[quote=[\"\']*(.*?)[\"\']*\](.*?)\[\/quote\]/ism",
373 "<br /><strong class=".'"author"'.">" . $t_wrote . "</strong><blockquote>$2</blockquote>",
376 // [img=widthxheight]image source[/img]
377 //$Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '<img src="$3" style="height: $2px; width: $1px;" >', $Text);
378 $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '<img src="$3" style="width: $1px;" >', $Text);
381 // [img]pathtoimage[/img]
382 $Text = preg_replace("/\[img\](.*?)\[\/img\]/ism", '<img src="$1" alt="' . t('Image/photo') . '" />', $Text);
386 $Text = preg_replace("/\[crypt\](.*?)\[\/crypt\]/ism",'<br/><img src="' .$a->get_baseurl() . '/images/lock_icon.gif" alt="' . t('Encrypted content') . '" title="' . t('Encrypted content') . '" /><br />', $Text);
387 $Text = preg_replace("/\[crypt=(.*?)\](.*?)\[\/crypt\]/ism",'<br/><img src="' .$a->get_baseurl() . '/images/lock_icon.gif" alt="' . t('Encrypted content') . '" title="' . '$1' . ' ' . t('Encrypted content') . '" /><br />', $Text);
392 $Text = preg_replace("/\[video\](.*?\.(ogg|ogv|oga|ogm|webm|mp4))\[\/video\]/ism", '<video src="$1" controls="controls" width="' . $a->videowidth . '" height="' . $a->videoheight . '"><a href="$1">$1</a></video>', $Text);
393 $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);
395 $Text = preg_replace_callback("/\[video\](.*?)\[\/video\]/ism", 'tryoembed', $Text);
396 $Text = preg_replace_callback("/\[audio\](.*?)\[\/audio\]/ism", 'tryoembed', $Text);
398 $Text = preg_replace("/\[video\](.*?)\[\/video\]/", '$1', $Text);
399 $Text = preg_replace("/\[audio\](.*?)\[\/audio\]/", '$1', $Text);
402 // html5 video and audio
406 $Text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/ism", '<iframe src="$1" width="' . $a->videowidth . '" height="' . $a->videoheight . '"><a href="$1">$1</a></iframe>', $Text);
408 $Text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/ism", '<a href="$1">$1</a>', $Text);
410 // Youtube extensions
412 $Text = preg_replace_callback("/\[youtube\](https?:\/\/www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", 'tryoembed', $Text);
413 $Text = preg_replace_callback("/\[youtube\](www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", 'tryoembed', $Text);
414 $Text = preg_replace_callback("/\[youtube\](https?:\/\/youtu.be\/.*?)\[\/youtube\]/ism",'tryoembed',$Text);
417 $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text);
418 $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/embed\/(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text);
419 $Text = preg_replace("/\[youtube\]https?:\/\/youtu.be\/(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text);
422 $Text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism", '<iframe width="' . $a->videowidth . '" height="' . $a->videoheight . '" src="http://www.youtube.com/embed/$1" frameborder="0" ></iframe>', $Text);
424 $Text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism", "http://www.youtube.com/watch?v=$1", $Text);
428 $Text = preg_replace_callback("/\[vimeo\](https?:\/\/player.vimeo.com\/video\/[0-9]+).*?\[\/vimeo\]/ism",'tryoembed',$Text);
429 $Text = preg_replace_callback("/\[vimeo\](https?:\/\/vimeo.com\/[0-9]+).*?\[\/vimeo\]/ism",'tryoembed',$Text);
432 $Text = preg_replace("/\[vimeo\]https?:\/\/player.vimeo.com\/video\/([0-9]+)(.*?)\[\/vimeo\]/ism",'[vimeo]$1[/vimeo]',$Text);
433 $Text = preg_replace("/\[vimeo\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/vimeo\]/ism",'[vimeo]$1[/vimeo]',$Text);
436 $Text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism", '<iframe width="' . $a->videowidth . '" height="' . $a->videoheight . '" src="http://player.vimeo.com/video/$1" frameborder="0" ></iframe>', $Text);
438 $Text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism", "http://vimeo.com/$1", $Text);
440 // $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);
444 $Text = oembed_bbcode2html($Text);
446 // Avoid triple linefeeds through oembed
447 $Text = str_replace("<br style='clear:left'></span><br /><br />", "<br style='clear:left'></span><br />", $Text);
449 // If we found an event earlier, strip out all the event code and replace with a reformatted version.
450 // Replace the event-start section with the entire formatted event. The other bbcode is stripped.
451 // Summary (e.g. title) is required, earlier revisions only required description (in addition to
452 // start which is always required). Allow desc with a missing summary for compatibility.
454 if((x($ev,'desc') || x($ev,'summary')) && x($ev,'start')) {
455 $sub = format_event_html($ev);
457 $Text = preg_replace("/\[event\-summary\](.*?)\[\/event\-summary\]/ism",'',$Text);
458 $Text = preg_replace("/\[event\-description\](.*?)\[\/event\-description\]/ism",'',$Text);
459 $Text = preg_replace("/\[event\-start\](.*?)\[\/event\-start\]/ism",$sub,$Text);
460 $Text = preg_replace("/\[event\-finish\](.*?)\[\/event\-finish\]/ism",'',$Text);
461 $Text = preg_replace("/\[event\-location\](.*?)\[\/event\-location\]/ism",'',$Text);
462 $Text = preg_replace("/\[event\-adjust\](.*?)\[\/event\-adjust\]/ism",'',$Text);
465 // Unhide all [noparse] contained bbtags unspacefying them
466 // and triming the [noparse] tag.
468 $Text = preg_replace_callback("/\[noparse\](.*?)\[\/noparse\]/ism", 'bb_unspacefy_and_trim',$Text);
469 $Text = preg_replace_callback("/\[nobb\](.*?)\[\/nobb\]/ism", 'bb_unspacefy_and_trim',$Text);
470 $Text = preg_replace_callback("/\[pre\](.*?)\[\/pre\]/ism", 'bb_unspacefy_and_trim',$Text);
473 $Text = preg_replace('/\[\&\;([#a-z0-9]+)\;\]/','&$1;',$Text);
474 $Text = preg_replace('/\&\#039\;/','\'',$Text);
475 $Text = preg_replace('/\"\;/','"',$Text);
477 // fix any escaped ampersands that may have been converted into links
478 $Text = preg_replace("/\<(.*?)(src|href)=(.*?)\&\;(.*?)\>/ism",'<$1$2=$3&$4>',$Text);
479 $Text = preg_replace("/\<(.*?)(src|href)=\"[^hfm](.*?)\>/ism",'<$1$2="">',$Text);
482 $Text = bb_replace_images($Text, $saved_image);
484 // Clean up the HTML by loading and saving the HTML with the DOM
485 // Only do it when it has to be done - for performance reasons
487 $doc = new DOMDocument();
488 $doc->preserveWhiteSpace = false;
490 $Text = mb_convert_encoding($Text, 'HTML-ENTITIES', "UTF-8");
492 $doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">';
493 @$doc->loadHTML($doctype."<html><body>".$Text."</body></html>");
495 $Text = $doc->saveHTML();
496 $Text = str_replace(array("<html><body>", "</body></html>", $doctype), array("", "", ""), $Text);
498 $Text = str_replace('<br></li>','</li>', $Text);
500 $Text = mb_convert_encoding($Text, "UTF-8", 'HTML-ENTITIES');
503 call_hooks('bbcode',$Text);