我正在研究一个bb-code parse函数,我遇到了问题,当它是[code][/code]
的情况时,我不想在这种情况下解析所有其他的bb-code-tags
实际上,功能是:
function bbcode_parse_view($string)
{
$tags = 'b|code|size|color|center|quote|url|img|video';
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 'code':
$replacement = "<pre>$innertext</pre>";
$is_bbcode_code = 1;
break;
case 'size':
$replacement = "<span style=\"font-size: $param;\">$innertext</span>";
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>";
break;
case 'url':
$replacement = '<a href="' . ($param ? $param : $innertext) . "\">$innertext</a>";
break;
case 'img':
list($width, $height) = preg_split('`[Xx]`', $param);
$replacement = "<div class=\"imgmediawrapper\"><img class=\"imgshadow\" src=\"$innertext\" " . (is_numeric($width) ? "width=\"$width\" " : '') . (is_numeric($height) ? "height=\"$height\" " : '') . '/></div>';
$is_bbcode_img = 1;
break;
case 'video':
$videourl = parse_url($innertext);
parse_str($videourl['query'], $videoquery);
if (strpos($videourl['host'], 'youtube.com') !== FALSE)
$replacement = '<div class="videomediawrapper imgshadow"><iframe src="//www.youtube-nocookie.com/embed/' . $videoquery['v'] . '?rel=0&theme=light&showinfo=0&iv_load_policy=3&modestbranding=1" frameborder="0"></iframe></div>';
if (strpos($videourl['host'], 'vimeo.com') !== FALSE)
$replacement = '<div class="videomediawrapper imgshadow"><iframe src="//player.vimeo.com/video' . $videourl['path'] . '?title=0&byline=0&portrait=0" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen>
</iframe></div>';
if (strpos($videourl['host'], 'google.com') !== FALSE)
$replacement = '<embed src="https://video.google.com/googleplayer.swf?docid=' . $videoquery['docid'] . '" width="400" height="326" type="application/x-shockwave-flash"></embed>';
$is_bbcode_video = 1;
break;
}
$string = str_replace($match, $replacement, $string);
}
}
请查看案例code
。如果发生这种情况,它应该忽略其余的情况,如果它们在'代码'案例中。
这有可能吗?
答案 0 :(得分:0)
难看!
但这是我在论坛上实现[nobbcode][/nobbcode]
的方法:
$string = preg_replace_callback("(\[nobbcode\](.*?)\[\/nobbcode\])is",
function($m) {return str_replace("[","[",$m[1]);}, $string);
// process all other codes here
这基本上是&#34;打破&#34;块内的任何BBCode,但由于它使用HTML实体,最终结果[与用户没有区别。