Guest
Public paste!

Untitled

By: a guest | Oct 6th, 2008 | Syntax: None | Size: 1.74 KB | Hits: 515 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. <?php
  2. function parse_bb($text)
  3. {
  4.         while (preg_match('#(?<!UNKNOWN:)\[(.+)(?:=(.*?))?\](.*?)\[\/\1\]#s',$text))
  5.         {
  6.                 $text = preg_replace_callback('#(?<!UNKNOWN:)\[(.+)(?:=(.*?))?\](.*?)\[\/\1\]#s', 'parse_bb_callback', $text);
  7.         }
  8.         return nl2br($text);
  9. }
  10. function parse_bb_callback($hit)
  11. {
  12.         $tag = $hit[1];
  13.         $attr = $hit[2];
  14.         $encl = $hit[3];
  15.  
  16.         switch ( $tag )
  17.         {
  18.                 case 'b': case 'li': case 'ol': case 'i': case 'u': case 'h3':
  19.                         return "<$tag>$encl</$tag>";
  20.                         break;
  21.                 case 'img':
  22.                                         return "<img src=\"$attr\" alt=\"$encl\" />";
  23.                         break;
  24.                 case 'lnk':
  25.                         if ( $encl == '' )
  26.                         {
  27.                                 return "<a href=\"$attr\" target=\"_blank\">$attr</a>";
  28.                         }
  29.                         return "<a href=\"$attr\">$encl</a>";
  30.                         break;
  31.                 case 'mailto':
  32.                         $mail = preg_replace( "/(.)/se", " '&#' . ord( '\\1' ) . ';' ", $attr );
  33.                         return "<a href=\"mailto:$mail\">$mail</a>";
  34.                         break;
  35.                 case 'html':
  36.                         $encl = html_entity_decode($encl);
  37.                         $encl = str_replace('&', '&amp;', $encl);
  38.                         return $encl;
  39.                         break;
  40.                 default:
  41.                         return "UNKNOWN:$hit[0]";
  42.         }
  43. }
  44. $html='[ol]
  45. [li]list item[/li]
  46. [li]another list item[/li]
  47. [/ol]
  48. [i][b]test[/b]ing[/i]
  49. [img=path]image[/img]
  50. [othertag=attr]encl[/othertag]';
  51. echo parse_bb($html);
  52. ?>