1
function bb_parse($string, $tags = 'b|i|u|quote|url|img|youtube|user') {
    $replacement = 0;
    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 'i': $replacement = '<em>'.$innertext.'</em>'; break;
            case 'u': $replacement = '<span style="text-decoration: underline;">'.$innertext.'</span>'; break;
            case 'quote': $replacement = '<div class="quote"><div class="quote-author">'.($param ? 'Quote by: '.$param : 'Quote').'</div>'.$innertext.'</div>'; break;
            case 'url': $replacement = '<a href="'.($param ? $param : $innertext).'">'.$innertext.'</a>'; break;
            case 'user': $replacement = '<a href="'.url('user/'.($param ? $param : $innertext)).'">'.$innertext.'</a>'; break;
            case 'img': $replacement = '<img src="'.$innertext.'" style="max-width: 640px;"/>'; break;
            case 'youtube':
            $videourl = parse_url($innertext);
            parse_str($videourl['query'], $videoquery);
            if (strpos($videourl['host'], 'youtube.com') !== FALSE) $replacement = '<div><iframe width="755" height="425" src="http://www.youtube.com/embed/'.$videoquery['v'].'" frameborder="0" allowfullscreen></iframe></div>';
            break;
        }
        $string = str_replace($match, $replacement, $string);
    }
    return $string;
}

たとえば、改行で [quote] を使用すると、次のように出力されます。

[quote]This is {LINEBREAK}
a test[/quote]

そして、これは想定どおりではありません

<div class="quote"><div class="quote-author">Quote</div>This is {LINEBREAK}
a test</div>

誰にもアイデアがありますか?

4

1 に答える 1

0

sRegExの修飾子を使用して、 「シングルラインモード」を通知できます。ドット(.)は、デフォルトでは改行文字と一致しません。s修飾子を使用して、それを行います。

preg_match_all('`\[('.$tags.')=?(.*?)\](.+?)\[/\1\]`s', $string, $matches)
于 2012-04-23T18:29:12.303 に答える