3

BBCodes を並べ替えようとしていますが、失敗しました

それで

[̶b̶]̶[̶i̶]̶[̶u̶]̶f̶o̶o̶[̶/̶b̶]̶[̶/̶u̶]̶[̶/̶i̶]̶ ̶-̶ ̶w̶r̶o̶n̶g̶ ̶o̶r̶d̶e̶r̶ ̶ ̶

私— —w—a—n—t— —i—t— —t—o— —b—e—:— —

̶[̶b̶]̶[̶i̶]̶[̶u̶]̶f̶o̶o̶[̶/̶u̶]̶[̶/̶i̶]̶[̶/̶b̶]̶ ̶-̶ ̶r̶i̶g̶h̶t̶ ̶o̶r̶d̶e̶r̶

写真:ここに画像の説明を入力

で試しました

<?php
$string = '[b][i][u]foo[/b][/u][/i]'; 
$search = array('/\[b](.+?)\[\/b]/is', '/\[i](.+?)\[\/i]/is', '/\[u](.+?)\[\/u]/is'); 
$replace = array('[b]$1[/b]', '[i]$1[/i]', '[u]$1[/u]'); 
echo preg_replace($search, $replace, $string); 
?>

出力: [b][i][u]foo[/b][/u][/i]

助言がありますか ?ありがとう!

4

1 に答える 1

0

ふぅ、これを行うためのロジックをしばらく考えました。(関数に入れても構いません)

これは、指定されたシナリオでのみ機能します。他のユーザーがコメントしているように、それは不可能です。あなたはこれをするべきではありません。またはサーバー側でも。構文エラーをスローするためだけに、クライアント側のパーサーを使用します。

サポート[b]a[i]b[u]foo[/b]baa[/u]too[/i]

およびカスタム値を持つ bbcode[url=test][i][u]foo[/url][/u][/i]

[b] bold [/b][u] underline[/u] Andで壊れます [b] bold [u][/b] underline[/u]

    //input string to be reorganized
    $string = '[url=test][i][u]foo[/url][/u][/i]';
    echo $string . "<br />";

    //search for all opentags (including ones with values
    $tagsearch = "/\[([A-Za-z]+)[A-Za-z=._%?&:\/-]*\]/";
    preg_match_all($tagsearch, $string, $tags);

    //search for all close tags to store them for later
    $closetagsearch = "/(\[\/([A-Za-z]+)\])/is";
    preg_match_all($closetagsearch, $string, $closetags);

    //flip the open tags for reverse parsing (index one is just letters)
    $tags[1] = array_reverse($tags[1]);
    //create temp var to store new ordered string
    $temp = "";
    //this is the last known position in the original string after a match
    $last = 0;
    //iterate through each char of the input string
    for ($i = 0, $len = strlen($string); $i < $len; $i++) {
        //if we run out of tags to replace/find stop looping
        if (empty($tags[1]) || empty($closetags[1]))
            continue;
        //this is the part of the string that has no matches
        $good = substr($string, $last, $i - $last);
        //next closing tag to search for
        $next = $closetags[1][0];
        //how many chars ahead to compare against
        $scope = substr($string, $i, strlen($next));
        //if we have a match
        if ($scope === "$next") {
            //add to the temp variable with a modified 
            //version of an open tag letter to become a close tag
            $temp .= $good . substr_replace("[" . $tags[1][0] . "]", "/", 1, 0);
            //remove the first key/value in both arrays
            array_shift($tags[1]);
            array_shift($closetags[1]);
            //update the last known unmatched char
            $last += strlen($good . $scope);
        }
    }

    echo $temp;

また、注意してください: タグを順不同でネストすることは、ユーザーの意図である可能性があります:X

于 2013-03-12T07:44:42.703 に答える