I am trying to convert the following 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>

to the following bbCode like this

[header]My Header1[header/]

[content]My content1[content/]

[header]My Header2[header/]

[content]My content2[content/] . . .

How can I do this ? I'm write this class but do not work

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;
   }

}

The problem is the collapseOne that for each pair of tags to be changed

有帮助吗?

解决方案

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>';
}

Thank

I've found the answer

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top