2011-08-15 18 views
0

我想製作內容列表就像在維基百科。我的代碼:PHP目錄

<?php 
$str = "[H1]Top Heading[/H1] My test string [H2]My sub Heading[/H2] My second [H3]My deep Heading[/H3] string now again [H1]Top Heading[/H1] My test string [H2]My sub Heading[/H2] My second [H3]My deep Heading[/H3]"; 

/** 
* Helper class 
*/ 
class FaqHelper { 
    static $count = 1; 
    static $listItems = array(); 
    static $prefix = 'faq-'; 

    static function GetList() { 

     $items = ''; 
      foreach (self::$listItems as $id => $label) { 
       $items .= '<li><a href="#' . self::$prefix . $id .'">' . $label . '</a></li>'; 
      } 

     return '<ul><ol>'. $items .'</ul></ol>'; 
    } 
} 

// the callback function 
function make_faq($matches) 
{ 
    $id = FaqHelper::$count; 
    $label = $matches[1]; 

    FaqHelper::$listItems[$id] = $label; 

    $res = '<span id="'. FaqHelper::$prefix . $id .'">' . $label . '</span>'; 

    FaqHelper::$count++; 

    return $res; 
} 

$text = preg_replace_callback(
    "#\[H1\]([^\[]+)\[/H1\]#", 
    "make_faq", 
    $str 
); 

$list = FaqHelper::GetList(); 

echo $list; 
echo '<br /><br />'; 
echo $text; 


?> 

,這給

  1. Top Heading 
     2. Top Heading 



Top Heading My test string [H2]My sub Heading[/H2] My second [H3]My deep Heading[/H3] string now again Top Heading My test string [H2]My sub Heading[/H2] My second [H3]My deep Heading[/H3] 

,但我想這於是就導致

 1. Top Heading 
     1.1 My sub Heading 
      1.1.1 My deep Heading 
    2. Top Heading 
     2.1 My sub Heading 
      2.1.1 My deep Heading 

... 頂部導坑我的測試字符串我的副標題我的第二個我深標題字符串現在再次頂部標題我的測試字符串我的子標題我的第二個我現在深的標題字符串現在再次

基本理論是這樣的。

當你打第一個[H1]時,你將計數器增加到1.當你點擊h2時,你的第二級計數器增加到1.當你點擊h3時,你的第三級計數器增加到1。

這顯然會輸出1.1.1。

現在當你擊中下一個h1時,他們的子級別(即不是主級別)被重置爲0,然後第一級別遞增到2.你點擊h2,然後再次到達1。 。然後你有第一級別2,子1,子1 = 2.1.1。

+0

文件..文件..在這裏的任何一個? – Hafiz

+0

你有什麼具體問題,你卡在哪裏? –

+0

iam非常新的PHP我不知道如何做到這一點,請幫助我 – Hafiz

回答

0

請注意,此代碼僅支持3個級別的標題。我知道這個解決方案不會無限擴展,並且不支持遞歸解析,但不幸的是我沒有時間爲SO問題創建完美的解決方案。

基於你最後一個問題的擴展解決方案如下。請測試它,看看它是否按預期工作。請注意,我已經在區分大小寫的情況下製作了與[H1] [/ H1]標籤匹配的正則表達式,因此[h1] [/ h1]現在也可以使用。

再一次,我很抱歉,如果我很慢或者結果不完美,但我不能完成整個項目,您需要自己做一些研究,學習,然後做。同樣,隨着代碼的發展和變厚,進行進一步的擴展和維護需要更多的時間,而我並不總是有能力去做。

享受!

<?php 

$str = " 
[h1]Header 1[/h1]Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam vitae rutrum tellus. Vestibulum nec nisl eu enim venenatis ornare. Suspendisse a nunc nibh, quis placerat urna. Pellentesque nec odio est. [h2]Header 2[/h2]Morbi et orci non nulla lobortis convallis. Curabitur vestibulum, felis auctor posuere posuere, mauris neque vulputate tellus, vel luctus urna sapien vitae sapien. [h2]Header 2-2[/h2] Integer accumsan lobortis euismod. Integer vitae tempor nisl. 

[h1]Header 1[/h1]Mauris varius dolor nec risus viverra egestas. Suspendisse potenti. Nunc consectetur faucibus metus, nec sagittis felis pellentesque non. Ut mattis ligula non [h2]Header 2[/h2] purus vehicula sed tempus risus porttitor. Nullam eget arcu[h3]Header 3[/h3] at mi pulvinar pellentesque eu sed ipsum. Fusce vel laoreet sem. Duis hendrerit ligula iaculis felis hendrerit euismod vel ut mauris. Pellentesque tempus velit et velit cursus sodales. Sed adipiscing vulputate nibh sed venenatis. Quisque sit amet ante velit. Donec dui lectus, ultricies quis interdum eget, elementum vitae nibh. Nullam nec sapien purus, laoreet porta elit. Nam luctus elit sit amet est ultricies eget varius diam ullamcorper. Sed commodo viverra lobortis. Vivamus semper blandit arcu semper pellentesque. Duis interdum tincidunt nisl eget mollis. 

[h1]Header 1[/h1]Suspendisse laoreet, tortor sed viverra pellentesque, enim felis tempus ante, sit amet dignissim leo augue non ipsum. Ut eu risus erat. Donec pellentesque ligula ac est tincidunt ullamcorper. [h2]My paragraph again[/h2] Pellentesque eget diam lacus, nec ornare urna. Nulla a interdum augue. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce et justo turpis. "; 

class FaqHeading 
{ 
    var $heading_level; 
    var $id; 
    var $label; 

    var $children = array(); 
    var $parent = null; 

    function __construct($heading_level, $id, $label) 
    { 
     $this->heading_level = (int) $heading_level; 
     $this->id = (int) $id; 
     $this->label = $label; 
    } 

    function AddChild(FaqHeading $child) 
    { 
      if ($child->heading_level <= $this->heading_level) { 
       return; // cannot add higher or equal leveld headings as children 
      } 

     $child->parent = $this; 
     $this->children[] = $child; 
    } 

    function HasChildren() 
    { 
     return (bool) $this->children; 
    } 
} 

class FaqHeadingList 
{ 
    var $headings = array(); 
    var $current = null; 
    var $prefix; 

    function __construct($prefix) 
    { 
     $this->prefix = $prefix; 
    } 

    function AddHeading($heading_level, $id, $label) 
    { 
     $elem = new FaqHeading($heading_level, $id, $label); 

      if ($this->current == null || $heading_level == 1) { 
       $this->headings[] = $elem; 
      } else if ($heading_level == 2 && ($this->current->heading_level == 1)) { 
       $this->current->AddChild($elem); 
      } else if ($heading_level == 2 && ($this->current->heading_level == 2)) { 
       $this->current->parent->AddChild($elem); 
      } else if ($heading_level == 2 && ($this->current->heading_level == 3)) { 
       $this->current->parent->parent->AddChild($elem); 
      } else if ($heading_level == 3 && ($this->current->heading_level == 1)) { 
       $this->current->AddChild($elem); 
      } else if ($heading_level == 3 && ($this->current->heading_level == 2)) { 
       $this->current->AddChild($elem); 
      } else if ($heading_level == 3 && ($this->current->heading_level == 3)) { 
       $this->current->parent->AddChild($elem); 
      } 

     $this->current = $elem; 
    } 

    function ToString() 
    { 
     $str = '<ol>'; 
      foreach ($this->headings as $h1) 
      { 
        if ($h1->heading_level == 1) 
        { 
         $str .= '<li>'. $this->CreateLink($h1->id, $h1->label); 
          if ($h1->hasChildren()) 
          { 
           $str .= '<ol>'; 
            foreach ($h1->children as $h2) 
            { 
             $str .= '<li>' . $this->CreateLink($h2->id, $h2->label); 

              if ($h2->hasChildren()) 
              { 
               $str .= '<ol>'; 
                foreach ($h2->children as $h3) 
                { 
                 $str .= '<li>'. $this->CreateLink($h3->id, $h3->label) .'</li>'; 
                } 
               $str .= '</ol>'; 
              } 
             $str .= '</li>'; 
            } 
           $str .= '</ol>'; 
          } 
         $str .= '</li>'; 
        } 
      } 
     $str .= '</ol>'; 

     return $str; 
    } 

    function CreateLink($id, $label) 
    { 
     return '<a href="#' . $this->prefix . $id .'">' . $label . '</a>'; 
    } 
} 

/** 
* Helper class 
*/ 
class FaqHelper 
{ 
    static $count = 1; 
    static $headingList; 
    static $prefix = 'faq-'; 

    static function Init() 
    { 
     self::$headingList = new FaqHeadingList(self::$prefix); 
    } 

    static function ReplaceCallback($matches) 
    { 
     $id = self::$count; 
     $heading_level = $matches[1]; 
     $label = $matches[2]; 

     self::$headingList->AddHeading($heading_level, $id, $label); 

     $res = '<span id="'. self::$prefix . $id .'">' . $label . '</span>'; 

     self::$count++; 

     return $res; 
    } 
} 

FaqHelper::init(); 

$text = preg_replace_callback(
    "#\[H([0-9]+)\]([^\[]+)\[/H\\1\]#i", 
    array('FaqHelper', "ReplaceCallback"), 
    $str 
); 

$list = FaqHelper::$headingList->ToString(); 

echo $list; 
echo '<br /><br />'; 
echo nl2br($text); 
?> 

應該輸出:

1. Header 1 
     1. Header 2 
     2. Header 2-2 
    2. Header 1 
     1. Header 2 
       1. Header 3 
    3. Header 1 
     1. My paragraph again 




Header 1Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam vitae rutrum tellus. Vestibulum nec nisl eu enim venenatis ornare. Suspendisse a nunc nibh, quis placerat urna. Pellentesque nec odio est. Header 2Morbi et orci non nulla lobortis convallis. Curabitur vestibulum, felis auctor posuere posuere, mauris neque vulputate tellus, vel luctus urna sapien vitae sapien. Header 2-2 Integer accumsan lobortis euismod. Integer vitae tempor nisl. 

Header 1Mauris varius dolor nec risus viverra egestas. Suspendisse potenti. Nunc consectetur faucibus metus, nec sagittis felis pellentesque non. Ut mattis ligula non Header 2 purus vehicula sed tempus risus porttitor. Nullam eget arcuHeader 3 at mi pulvinar pellentesque eu sed ipsum. Fusce vel laoreet sem. Duis hendrerit ligula iaculis felis hendrerit euismod vel ut mauris. Pellentesque tempus velit et velit cursus sodales. Sed adipiscing vulputate nibh sed venenatis. Quisque sit amet ante velit. Donec dui lectus, ultricies quis interdum eget, elementum vitae nibh. Nullam nec sapien purus, laoreet porta elit. Nam luctus elit sit amet est ultricies eget varius diam ullamcorper. Sed commodo viverra lobortis. Vivamus semper blandit arcu semper pellentesque. Duis interdum tincidunt nisl eget mollis. 

Header 1Suspendisse laoreet, tortor sed viverra pellentesque, enim felis tempus ante, sit amet dignissim leo augue non ipsum. Ut eu risus erat. Donec pellentesque ligula ac est tincidunt ullamcorper. My paragraph again Pellentesque eget diam lacus, nec ornare urna. Nulla a interdum augue. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce et justo turpis. 
+0

你是對的,我需要先學習所有這些東西,然後從現在開始項目我會學習這個謝謝建議和代碼 – Hafiz