php
  • nested
  • bbcode
  • quote
  • 2012-08-11 76 views 0 likes 
    0

    我想在我的BBcode類中實現嵌套引號。但還沒有成功。PHP不能讓嵌套引號工作

    這是代碼即時試圖實現:

    $string = ' 
    [quote="test"] 
        [quote="abc"]Test[/quote] 
        test 
    [/quote] 
    Hello 
    '; 
    
    function parseTagsRecursive($input) 
    { 
    
        $regex = '#\[quote="(.*?)"]((?:[^[]|\[(?!/?quote=""])|(?R))+)\[/quote]#'; 
    
        if (is_array($input)) { 
         $input = '<div style="background:#282828; padding:0; color:white;"> 
         <span style="display:block; background:#161616; margin-top:0; padding:5px;">' . $input[1] . ' wrote</span> 
         <span style="display:block; padding:5px; font-style:italic; font-size:12px;">'. $input[2] . '</span> 
        </div>'; 
        } 
    
        return preg_replace_callback($regex, 'parseTagsRecursive', $input); 
    } 
    
    $output = parseTagsRecursive($string); 
    
    echo $output; 
    

    而且這是我走到這一步:

    class BBCode { 
    
        public $str; 
    
        function parse() { 
    
         $this->str = preg_replace_callback(
           '#\[quote="(.*?)"]((?:[^[]|\[(?!/?quote=""])|(?R))+)\[/quote]#', 
           array($this, 'nestedQuotes'), 
           $this->str); 
    
         return $this->str; 
    
        } 
    
        function nestedQuotes($input) { 
         if (is_array($input)) { 
          $input = '<div style="background:#282828; padding:0; color:white;"> 
          <span style="display:block; background:#161616; margin-top:0; padding:5px;">' . $input[1] . ' wrote</span> 
          <span style="display:block; padding:5px; font-style:italic; font-size:12px;">'. $input[2] . '</span> 
         </div>'; 
         } 
         return $input; 
        } 
    
    } 
    
    $string = ' 
    [quote="test"] 
        [quote="abc"]Test[/quote] 
        test 
    [/quote] 
    Hello 
    '; 
    
    $b = new BBCode(); 
    $b->str = $string; 
    echo $b->parse(); 
    

    我希望有人能夠幫助這一點。我搜索了很多,但沒有找到任何解決方案的問題。

    回答

    0

    知道它現在的工作。

    class BBCode { 
        function parse() { 
         $this->input = $this->nestedQuotes2($this->input); 
        } 
    
        function nestedQuotes2($input) { 
         if (is_array($input)) 
          $input = '<div class="quote"><h2>' . $input[1] . ' skrev</h2><span class="txt">' . $input[2] . '</span></div>'; 
         return preg_replace_callback('#\[quote="(.*?)"]((?:[^[]|\[(?!/?quote=""])|(?R))+)\[/quote]#', array($this, 'nestedQuotes2'), $input); 
        } 
    } 
    
    0

    我認爲你正在尋找heredoc

    相關問題