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

标签 php regex preg-replace

我正在尝试运行 preg_replace() 函数来替换字符串/内容中两个自定义标记(即 [xcode])之间的内容页。

我想要对这些自定义标记之间的内容执行的操作是通过 highlight_string() 函数运行它,并从输出中删除这些自定义标记。

知道如何做吗?

最佳答案

所以你需要一个 BBCode 解析器。下面的示例将 [xcode] 标记替换为您喜欢的任何标记。

<?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);
?>

使用preg_replace_callback()如果您想将匹配的文本传递给函数:

<?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);
?>

我将包含我很久以前制作的 BBCode 解析器的源代码。请随意使用它。

<?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("[\n]", $points[$i]);
            $brs = count($nls) - 2;
            $points[$i] = preg_replace("[\r\n]", "<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("[\r\n]", "", $text);
    }
    return $text;
}
function bbcode_parse($text) {
    $text = preg_replace("[\r\n]", "<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;
}
?>

关于php - 如何使用 preg_replace() 将 hilight_string() 应用于类似 BBCode 标签之间的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6361459/

相关文章:

java - 如何在 Java 中使用 Oracle 正则表达式或如何映射它们?

php - 如何删除另一个div中的唯一div标签

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

php - 如何解决此警告 : mysqli_fetch_assoc() expect s parameter 1 to be mysqli_result, 如何解决此问题

php - WP环境下内连接SQL,where id=id

php - preg_replace : wildcards do not match umlaut-characters

php - 密码字段正则表达式

php - PHP 中的 _($string) 函数有什么作用?

c++ - 正则表达式 VS13 : replace backslash

javascript - 从变量中的字符串转义JavaScript中的双引号