2012-08-11 10 views
0

내 BBcode 클래스에서 중첩 된 따옴표를 구현하려고합니다. 그러나 havent 아직 성공합니다. 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); 
    } 
}