0

我正在尝试转换以下html

<div id="collapseOne" class="accordion-body collapse">
<div class="accordion-inner">
        My Header1
</div></div>

<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseOne">
        My content1
</a></div>

像这样到下面的bbCode

[标题]我的标题1[标题/]

[内容]我的内容1[内容/]

[标题]我的标题2[标题/]

[内容]我的内容2[内容/]。. .

我怎样才能做到这一点 ?我正在写这门课但不工作

class BBCode
{
   private $Tags = array(
      'content','header');

   /**
    * Replaces all the BBCodes to HTML Codes
    * param string $text: the text with BBCodes
    */
   function ReplaceTag($Text)
   {
       $Replace_code['content'] = "<div id=\"collapseOne\" class=\"accordion-body collapse in\"><div class=\"accordion-inner\">$1</div></div>" ;
       $Replace_code['header'] = "<div class=\"accordion-heading\"><a class=\"accordion-toggle\" data-toggle=\"collapse\" data-parent=\"#accordion2\" href=\"#collapseOne\">$1</a></div>" ;

      // Deleting spaces from begging and end of string
      $Done = trim($Text);

      // Deleting all html code
      $Done = htmlspecialchars($Done);
      if(in_array("content",$this->Tags))
         $Done = preg_replace("/\[content\](.*?)\[\/content\]/is", $Replace_code['content'], $Done);

      if(in_array("header",$this->Tags))
           $Done = preg_replace("(\[header\](.*?)\[\/header\])is", $Replace_code['header'], $Done);

        $Done = nl2br($Done);
    enter code here
        return $Done;
   }

}

问题是collapseOne,每对标签都要改变

4

1 回答 1

0
function Acco_Tag($Text)
{
    $Text = preg_replace_callback('#\[header\](.*?)\[/header\]#muis','iNext4header', $Text);
    return preg_replace_callback('#\[content\](.*?)\[/content\]#muis', 'iNext4content', $Text);
}
private function iNext4header($match)
{
    static $i=0;
    return '<div class="accordion-heading"><a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapse'.(++$i).'">'.$match[1].'</a></div>';
}
private function iNext4content($match)
{
    static $i=0;
    return '<div id="collapse'.(++$i).'" class="accordion-body collapse in"><div class="accordion-inner">'.$match[1].'</div></div>';
}

感谢

我找到了答案

于 2013-09-09T08:40:03.157 回答