douqi3913 2011-06-15 17:03
浏览 79
已采纳

如何使用preg_replace()将hilight_string()应用于类似BBCode的标签之间的内容?

I'm trying to run the preg_replace() function to replace the content in between two custom tags (i.e. [xcode]) within the string / content of the page.

What I want to do with the content between these custom tags is to run it through highlight_string() function and to remove those custom tags from the output.

Any idea how to do it?

  • 写回答

4条回答 默认 最新

  • doubu8643 2011-06-15 17:29
    关注

    So you want sort of a BBCode parser. The example below replaces [xcode] tags with whatever markup you like.

    <?php
    function highlight($text) {
        $text = preg_replace('#\[xcode\](.+?)\[\/xcode\]#msi', '<em>\1</em>', $text);
        return $text;
    }
    $text = '[xcode]Lorem ipsum[/xcode] dolor sit [xcode]amet[/xcode].';
    echo highlight($text);
    ?>
    

    Use preg_replace_callback() if you want to pass the matched text to a function:

    <?php
    function parse($text) {
        $text = preg_replace_callback('#\[xcode\](.+?)\[\/xcode\]#msi',
            function($matches) {
                return highlight_string($matches[1], 1);
            }
        , $text);
        return $text;
    }
    $text = '[xcode]Lorem ipsum[/xcode] dolor sit [xcode]amet[/xcode].';
    echo bbcode($text);
    ?>
    

    I'll include the source code of a BBCode parser that I made a long time ago. Feel free to use it.

    <?php
    function bbcode_lists($text) {
        $pattern = "#\[list(\=(1|a))?\](.*?)\[\/list\]#msi";
        while (preg_match($pattern, $text, $matches)) {
            $points = explode("[*]", $matches[3]);
            array_shift($points);
            for ($i = 0; $i < count($points); $i++) {
                $nls = split("[
    ]", $points[$i]);
                $brs = count($nls) - 2;
                $points[$i] = preg_replace("[
    ]", "<br />", $points[$i], $brs);
            }
            $replace = ($matches[2] != '1') ? ($matches[2] != 'a') ? '<ul>' : '<ol style="list-style:lower-alpha">' : '<ol style="list-style:decimal">';
            $replace .= "<li>";
            $replace .= implode("</li><li>", $points);
            $replace .= "</li>";
            $replace .= ($matches[2] == '1' || $matches[2] == 'a' ) ? '</ol>' : '</ul>';
            $text = preg_replace($pattern, $replace, $text, 1);
            $text = preg_replace("[
    ]", "", $text);
        }
        return $text;
    }
    function bbcode_parse($text) {
        $text = preg_replace("[
    ]", "<br />", $text);
        $smilies = Array(
            ':)' => 'smile.gif',
            ':d' => 'tongue2.gif',
            ':P' => 'tongue.gif',
            ':lol:' => 'lol.gif',
            ':D' => 'biggrin.gif',
            ';)' => 'wink.gif',
            ':zzz:' => 'zzz.gif',
            ':confused:' => 'confused.gif'
        );
        foreach ($smilies as $key => $value) {
            $text = str_replace($key, '<img src="/images/smilies/' . $value . '" alt="' . $key . '" />', $text);
        }
        if (!(!strpos($text, "[") && !strpos($text, "]"))) {
            $bbcodes = Array(
                '#\[b\](.*?)\[/b\]#si' => '<strong>$1</strong>',
                '#\[i\](.*?)\[/i\]#si' => '<em>$1</em>',
                '#\[u\](.*?)\[/u\]#si' => '<span class="u">$1</span>',
                '#\[s\](.*?)\[/s\]#si' => '<span class="s">$1</span>',
                '#\[size=(.*?)\](.*?)\[/size\]#si' => '<span style="font-size:$1">$2</span>',
                '#\[color=(.*?)\](.*?)\[/color\]#si' => '<span style="color:$1">$2</span>',
                '#\[url=(.*?)\](.*?)\[/url\]#si' => '<a href="$1" target="_blank">$2</a>',
                '#\[url\](.*?)\[/url\]#si' => '<a href="$1" target="_blank">$1</a>',
                '#\[img\](.*?)\[/img\]#si' => '<img src="$1" alt="" />',
                '#\[code\](.*?)\[/code\]#si' => '<div class="code">$1</div>'
            );
            $text = preg_replace(array_keys($bbcodes), $bbcodes, $text);
    
            $text = bbcode_lists($text);
    
            $quote_code = Array("'\[quote=(.*?)\](.*?)'i", "'\[quote](.*?)'i", "'\[/quote\]'i");
            $quote_html = Array('<blockquote><p class="quotetitle">Quote \1:</p>\2', '<blockquote>\2', '</blockquote>');
            $text = preg_replace($quote_code, $quote_html, $text);
    
        }
        return $text;
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度