公告板转换出错

时间:2016-06-14 19:30:08

标签: php bbcode

我从PHP文档(PHP: bbcode_create)复制了这个文本,并将其修改为快速用于BB代码。但是,只要两个BB代码之间存在换行符,例如。在[b][/b]之间,它不会转换代码。

以下是我使用的代码:

function bb_codes($string) { 
        $tags = 'b|i|size|color|center|quote|url|img|li|list|s'; 
        while (preg_match_all('`\[('.$tags.')=?(.*?)\](.+?)\[/\1\]`', $string, $matches)) foreach ($matches[0] as $key => $match) { 
            list($tag, $param, $innertext) = array($matches[1][$key], $matches[2][$key], $matches[3][$key]); 
            switch ($tag) { 
                case 'b': $replacement = "<strong>$innertext</strong>"; break; 
                case 's': $replacement = "<strike>$innertext</strike>"; break; 
                case 'i': $replacement = "<em>$innertext</em>"; break; 
                case 'size': $replacement = "<span style=\"font-size: $param;\">$innertext</span>"; break; 
                 case 'list': if(!empty($param)){ $replacement = "<ol>$innertext</ol>"; } else{ $replacement = "<ul>$innertext</ul>"; } break; 
                 case 'li': $replacement = "<li>$innertext</li>"; break;    
                case 'color': $replacement = "<span style=\"color: $param;\">$innertext</span>"; break; 
                case 'center': $replacement = "<div class=\"centered\">$innertext</div>"; break; 
                case 'quote': $replacement = "<blockquote>$innertext</blockquote>" . $param? "<cite>$param</cite>" : ''; break; 
                case 'url': $replacement = '<a href="' . ($param? $param : $innertext) . "\">$innertext</a>"; break; 
                case 'img': 
                    list($width, $height) = preg_split('`[Xx]`', $param); 
                    $replacement = "<img src=\"$innertext\" " . (is_numeric($width)? "width=\"$width\" " : '') . (is_numeric($height)? "height=\"$height\" " : '') . '/>'; 
                break; 
                case 'video': 
                    $videourl = parse_url($innertext); 
                    parse_str($videourl['query'], $videoquery); 
                    if (strpos($videourl['host'], 'youtube.com') !== FALSE) $replacement = '<embed src="http://www.youtube.com/v/' . $videoquery['v'] . '" type="application/x-shockwave-flash" width="425" height="344"></embed>'; 
                    if (strpos($videourl['host'], 'google.com') !== FALSE) $replacement = '<embed src="http://video.google.com/googleplayer.swf?docid=' . $videoquery['docid'] . '" width="400" height="326" type="application/x-shockwave-flash"></embed>'; 
                break; 
            } 
            $string = str_replace($match, $replacement, $string); 
        } 
        return $string; 
    } 

提前致谢!

P.S:我仅编辑了slistli

1 个答案:

答案 0 :(得分:0)

问题在于,在标准(即无修饰符)正则表达式中,public Stream<Map.Entry<String, Integer>> foos() { map.entrySet().stream() } 元字符与行中断不匹配。解决方法是使用public Optional<Integer> fooOf(String key) { return Optional.ofNullable(map.get(key)); } 修饰符,使匹配换行符,而不是

.

在第3行的s中,使用:

'`\[('.$tags.')=?(.*?)\](.+?)\[/\1\]`'