preg_match_all开关案例函数中的if-condition

时间:2014-04-16 15:42:48

标签: php foreach switch-statement

我正在研究一个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&amp;theme=light&amp;showinfo=0&amp;iv_load_policy=3&amp;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&amp;byline=0&amp;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。如果发生这种情况,它应该忽略其余的情况,如果它们在'代码'案例中。

这有可能吗?

1 个答案:

答案 0 :(得分:0)

难看!

但这是我在论坛上实现[nobbcode][/nobbcode]的方法:

$string = preg_replace_callback("(\[nobbcode\](.*?)\[\/nobbcode\])is",
  function($m) {return str_replace("[","&#91;",$m[1]);}, $string);
// process all other codes here

这基本上是&#34;打破&#34;块内的任何BBCode,但由于它使用HTML实体,最终结果[与用户没有区别。