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

发布于 2024-11-15 17:38:46 字数 197 浏览 2 评论 0原文

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

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

知道怎么做吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情仇皆在手 2024-11-22 17:38:46

所以你需要一个 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;
}
?>

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("[\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;
}
?>
十六岁半 2024-11-22 17:38:46

基本上,

 preg_replace_callback('~\[tag\](.+?)\[/tag\]~', function($matches) { whatever }, $text);

这不处理嵌套标签,尽管

完整的示例

$text = "hello [xcode] <? echo bar ?> [/xcode] world";

echo preg_replace_callback(
    '~\[xcode\](.+?)\[/xcode\]~', 
    function($matches) { 
        return highlight_string($matches[1], 1);
    }, 
    $text
);

basically,

 preg_replace_callback('~\[tag\](.+?)\[/tag\]~', function($matches) { whatever }, $text);

this doesn't handle nested tags though

complete example

$text = "hello [xcode] <? echo bar ?> [/xcode] world";

echo preg_replace_callback(
    '~\[xcode\](.+?)\[/xcode\]~', 
    function($matches) { 
        return highlight_string($matches[1], 1);
    }, 
    $text
);
末骤雨初歇 2024-11-22 17:38:46
<?php
$string = 'The quick brown fox jumped over the lazy dog.';
$patterns = array();
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$replacements = array();
$replacements[2] = 'bear';
$replacements[1] = 'black';
$replacements[0] = 'slow';
echo preg_replace($patterns, $replacements, $string);
?>

上面的例子将输出:

The bear black Slowly Jumped over the Lazy Dog。

http://php.net/manual/en/function.preg-replace.php

或者

str_replace 应该可以帮助你

http://php.net/manual/en/function.str-replace.php

<?php
$string = 'The quick brown fox jumped over the lazy dog.';
$patterns = array();
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$replacements = array();
$replacements[2] = 'bear';
$replacements[1] = 'black';
$replacements[0] = 'slow';
echo preg_replace($patterns, $replacements, $string);
?>

The above example will output:

The bear black slow jumped over the lazy dog.

http://php.net/manual/en/function.preg-replace.php

OR

str_replace should help you

http://php.net/manual/en/function.str-replace.php

﹎☆浅夏丿初晴 2024-11-22 17:38:46

感谢 user187291 的建议和 preg_replace_callback 规范 我最终得到了下面的结果是正确的! :

function parseTagsRecursive($input)
{

    $regex = '~\[xcode\](.+?)\[/xcode\]~';

    if (is_array($input)) {
        $input = highlight_string($input[1], true);
    }

    return preg_replace_callback($regex, 'parseTagsRecursive', $input);
}


$text = "hello [xcode] <? echo bar ?> [/xcode] world and [xcode] <?php phpinfo(); ?> [/xcode]";

echo parseTagsRecursive($text);

通过该函数解析$text变量的输出是:

hello  <? echo bar ?>  world and  <?php phpinfo(); ?>  

谢谢大家的输入!

Thanks to user187291's suggestion and preg_replace_callback specification I've ended up with the following outcome which does the job spot on! :

function parseTagsRecursive($input)
{

    $regex = '~\[xcode\](.+?)\[/xcode\]~';

    if (is_array($input)) {
        $input = highlight_string($input[1], true);
    }

    return preg_replace_callback($regex, 'parseTagsRecursive', $input);
}


$text = "hello [xcode] <? echo bar ?> [/xcode] world and [xcode] <?php phpinfo(); ?> [/xcode]";

echo parseTagsRecursive($text);

The output of parsing the $text variable through this function is:

hello  <? echo bar ?>  world and  <?php phpinfo(); ?>  

Thank you everyone for input!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文