0

我有这样的事情:

[shortcode] text [/shortcode] [shortcode_2] text [/shortcode_2][button] [shortcode_3] text [/shortcode_3] [image] text

我如何 preg_replace (或 str_replace )以便在每个或<br />之间插入 '][''] ['

编辑:

为了让事情尽可能的清楚...

输入:

[shortcode] text [/shortcode] [shortcode_2] text [/shortcode_2][button] [shortcode_3] text [/shortcode_3] [image] text

输出:

[shortcode]<br />text<br />[/shortcode]<br />[shortcode_2]<br />text<br />[/shortcode_2]<br />[button]<br />[shortcode_3]<br />text<br />[/shortcode_3]<br />[image]<br />text
4

1 回答 1

1

使用preg_replace_callback

$r = preg_replace_callback('/\]([^\]]+)?(\[)|([^\]]+$)/', function($matches) {

    var_dump($matches);

    if (!strlen($matches[1])) {
        return "";
    } else if (!isset($matches[1]) || !strlen(trim($matches[1]))) {
        return "]<br />[";
    } else {
        return "]<br />" . trim($matches[1]) . "<br />[";
    }

}, '[shortcode] text [/shortcode] [shortcode_2] text [/shortcode_2][button] [shortcode_3] text [/shortcode_3] [image] text');
于 2013-10-20T10:28:08.080 回答