I'm working on a bb-code parse function, I am stuck with the problem, that when it's a [code][/code]
case, I don't want to parse all other bb-code-tags within this case.
Actually, the function is:
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);
}
}
Just have a look at the case code
please. If this happens, it should ignore the rest of cases, if they are inside the 'code' case.
Is this somehow possible?
Ugly!
But here's how I implement [nobbcode][/nobbcode]
over on my forums:
$string = preg_replace_callback("(\[nobbcode\](.*?)\[\/nobbcode\])is",
function($m) {return str_replace("[","[",$m[1]);}, $string);
// process all other codes here
This basically "breaks" any BBCodes inside the block, but since it's using the HTML entity the end result [ is no different to the user.