From 5baebf61ac2f8d0ca060373a3709fc6698afd70b Mon Sep 17 00:00:00 2001 From: neoraider Date: Wed, 24 May 2006 23:58:04 +0000 Subject: BBCode-Parser neu geschrieben. --- code/bbcode.inc.php | 258 +++++++++++++++------------------------------------- 1 file changed, 74 insertions(+), 184 deletions(-) (limited to 'code/bbcode.inc.php') diff --git a/code/bbcode.inc.php b/code/bbcode.inc.php index 5d213f4..b5e0b05 100644 --- a/code/bbcode.inc.php +++ b/code/bbcode.inc.php @@ -1,201 +1,91 @@ '\[b\]', 'endtag' => '\[/b\]', + 'starthtml' => '', 'endhtml' => '', + 'children' => array(&$b, &$u, &$i, &$color, &$img, &$url1, &$url2)); + $u = array('starttag' => '\[u\]', 'endtag' => '\[/u\]', + 'starthtml' => '', 'endhtml' => '', + 'children' => array(&$b, &$u, &$i, &$color, &$img, &$url1, &$url2)); + $i = array('starttag' => '\[i\]', 'endtag' => '\[/i\]', + 'starthtml' => '', 'endhtml' => '', + 'children' => array(&$b, &$u, &$i, &$color, &$img, &$url1, &$url2)); + $color = array('starttag' => '\[color=([A-Za-z]+|#(?:[\dA-Fa-f]{3}){1,2}?|rgb\((?:100%|\d?\d%),(?:100%|\d?\d%),(?:100%|\d?\d%)\)|rgb\((?:1?\d?\d|2(?:[0-4]\d|5[0-5])),(?:1?\d?\d|2(?:[0-4]\d|5[0-5])),(?:1?\d?\d|2(?:[0-4]\d|5[0-5]))\))\]', 'endtag' => '\[/color\]', + 'starthtml' => '', 'endhtml' => '', + 'children' => array(&$b, &$u, &$i, &$color, &$img, &$url1, &$url2)); + $img = array('starttag' => '\[img\]([^\[]*)\[/img\]', 'starthtml' => ''); + $url1 = array('starttag' => '\[url\]([^\[]*)\[/url\]', 'starthtml' => '$1)'); + $url2 = array('starttag' => '\[url=([^\]]*)\]', 'endtag' => '\[/url\]', + 'starthtml' => '', 'endhtml' => '', + 'children' => array(&$b, &$u, &$i, &$color, &$img, &$url1, &$url2)); + $list1 = array('starttag' => '\[list\]\s*\[\*\]', 'endtag' => '\[/list\]', + 'starthtml' => '', + 'children' => array(&$b, &$u, &$i, &$color, &$img, &$url1, &$url2, &$list1, &$list2, &$list3, &$star)); + $list2 = array('starttag' => '\[list=1\]\s*\[\*\]', 'endtag' => '\[/list\]', + 'starthtml' => '
  1. ', 'endhtml' => '
', + 'children' => array(&$b, &$u, &$i, &$color, &$img, &$url1, &$url2, &$list1, &$list2, &$list3, &$star)); + $list3 = array('starttag' => '\[list=a\]\s*\[\*\]', 'endtag' => '\[/list\]', + 'starthtml' => '
  1. ', 'endhtml' => '
', + 'children' => array(&$b, &$u, &$i, &$color, &$img, &$url1, &$url2, &$list1, &$list2, &$list3, &$star)); + $star = array('starttag' => '\[\*\]', 'starthtml' => '
  • '); - $input = strtr($input, array('<' => '<', '>' => '>', '&' => '&', '"' => '"', "\n" => '
    ', "\r" => '')); - if($input[0] == '[') { - strtok(' ' . $input, '['); - $out = ''; - } - else $out = strtok($input, '['); + $this->codes = array(&$b, &$u, &$i, &$color, &$img, &$url1, &$url2, &$list1, &$list2, &$list3); + } + + function Parse($data) { + $data = htmlentities($data); + $out = ''; + $tags = array(); - while($part = strtok('[')) { - if($tmpok) { - if(substr($part, 0, strlen($tmpend)) == $tmpend) { - $out .= $tmpok . substr($part, strlen($tmpend)); - $tags = $tmptag . $tags; - $lists = $tmplist . $lists; - $tl = strcspn($tags, ']'); - $tmpok = ''; - $tmpnok = ''; - $tmptag = ''; - $tmpend = ''; - $tmplist = ''; - continue; - } - else { - $out .= '[' . $tmpnok; - $tmpok = ''; - $tmpnok = ''; - $tmptag = ''; - $tmpend = ''; - $tmplist = ''; - } - } - - if(substr($part, 0, 2) == 'b]') { - $out .= '' . substr($part, 2); - $tags = '[/b]' . $tags; - } - elseif(substr($part, 0, 2) == 'i]') { - $out .= '' . substr($part, 2); - $tags = '[/i]' . $tags; - } - elseif(substr($part, 0, 2) == 'u]') { - $out .= '' . substr($part, 2); - $tags = '[/u]' . $tags; - } - elseif(substr($part, 0, 5) == 'size=') { - $size = intval(substr($part, 5)); - if($size == 9 && $part[6] == ']') { - $out .= '' . substr($part, 7); - $tags = '[/size]' . $tags; - } - elseif(($size >= 10 && $size <= 24) && $part[7] == ']') { - $out .= '' . substr($part, 8); - $tags = '[/size]' . $tags; - } - else $out .= '[' . $part; - } - elseif(substr($part, 0, 6) == 'color=') { - $l = strspn(substr($part, 6), 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890#(),%'); - if($part[$l+6] == ']') { - $out .= '' . substr($part, $l+7); - $tags = '[/color]' . $tags; - } - else $out .= '[' . $part; - } - elseif(substr($part, 0, 5) == 'code]') { - $out .= '
    Code:
    ' . substr($part, 5); - $tags = '[/code]' . $tags; - } - elseif(substr($part, 0, 4) == 'url]') { - $tmpok = '' . substr($part, 4) . ''; - $tmpnok = $part; - $tmpend = '/url]'; - } - elseif(substr($part, 0, 4) == 'url=') { - $l = strcspn(substr($part, 4), '[] '); - if($part[$l+4] == ']') { - $out .= '' . substr($part, $l+5); - $tags = '[/url]' . $tags; - } - else $out .= '[' . $part; - } - elseif(substr($part, 0, 6) == 'email]') { - $tmpok = '' . substr($part, 6) . ''; - $tmpnok = $part; - $tmpend = '/email]'; - } - elseif(substr($part, 0, 6) == 'email=') { - $l = strcspn(substr($part, 6), '[] '); - if($part[$l+6] == ']') { - $out .= '' . substr($part, $l+7); - $tags = '[/email]' . $tags; - } - else $out .= '[' . $part; - } - elseif(substr($part, 0, 4) == 'img]') { - $tmpok = ''; - $tmpnok = $part; - $tmpend = '/img]'; - } - elseif(substr($part, 0, 5) == 'list]') { - $tmpok = substr($part, 5) . '
    '; - break; - case 'list': - $out .= '
  • '; - $lists = substr($lists, 1); - } + $data = $match[count($match)-1]; + + array_pop($tags); - $out .= substr($part, $tl); + continue; } - else $out .= '[' . $part; - $tl = strcspn($tags, ']'); - } - - if($tmpok) $out .= '[' . $tmpnok; - - $part = strtok($tags, '['); - - while($part) { - $tags = substr($tags, $tl+1); - - switch(substr($part, 1, $tl-2)) { - case 'b': - case 'i': - case 'u': - case 'size': - case 'color': - $out .= ''; - break; - case 'url': - case 'email': - $out .= ''; - break; - case 'code': - $out .= ''; + if($tags) $codes = &$tags[count($tags)-1]['children']; + else $codes = &$this->codes; + + $found = false; + + foreach($codes as $code) { + if(preg_match('(^([^\[]*)(' . $code['starttag'] . ')(.*)$)s', $data, $match)) { + $out .= $match[1]; + $out .= preg_replace('(^' . $code['starttag'] . '$)s', $code['starthtml'], $match[2]); + + $data = $match[count($match)-1]; + + if($code['endtag']) + array_push($tags, $code); + + $found = true; + break; - case 'list': - $out .= ''; - $lists = substr($lists, 1); + } } - $tl = strcspn($tags, ']'); + if($found) continue; + + if(!preg_match('(^([^\[]*)\[(.*)$)s', $data, $match)) break; - $part = strtok('['); + $out .= $match[1] . '['; + $data = $match[2]; } - return $out; + $out .= $data; + + while($tag = array_pop($tags)) + $out .= $tag['endhtml']; + + return strtr($out, array("\n" => '
    ', "\r" => '')); } } -- cgit v1.2.3