所以我继续回答我自己的问题并创建了一个目录,它可以工作,它会吐出我想要的所有内容,接受锚标签不起作用。它不会跳转到相应的 h1-6 标签所在的页面。
我有以下 php:
public function table_of_contents(&$content, $HeaderParameter){
//Creat Empty variables
$HeaderNums = "";
$ContentLink = "";
$IndentLast = 1;
//Creates a single string of header identifier. eg: "1234"
foreach($HeaderParameter as $Num){
$HeaderNums.= $Num;
}
//Setup header to search for our headers specified by user
if (preg_match_all('/<h(['.$HeaderNums.'])(.*?)>(.*?)(<\/h['.$HeaderNums.']>)/', $content, $Result)){
// Start Table
$ContentLink.="<ul id='TB_UL'>";
// Go through each result and add to our list
foreach ($Result[0] as $key => $title){
//Get header text
$HeaderText = strip_tags($Result[0][$key]);
// If user assign an ID then get it so that we can add our on
$TagIdRegexOutput = split('"',$Result[2][$key]);
// Check if user has already set an id, if so use theres
if($TagIdRegexOutput[0]){
$TagRef = $TagIdRegexOutput[1];
}
else{
$TagRef = $HeaderText;
}
//Set a level.
$IndentPosCurrent = $Result[1][$key];
//Create link to header
$ContentLink.='<li class="TB_Level' . $Result[1][$key] .'"><a class="TB_Link" href="#'.$TagRef.'">'.$HeaderText.'</a>'.'</li>';
// Create header tag
$HeaderTag = "h".$Result[1][$key];
// Replace header in content with our assign id
$content = str_replace($Result[0][$key], "<$HeaderTag"." id='$TagRef' ".">$HeaderText</$HeaderTag>", $content);
}
// End List
$ContentLink.="</ul> <!-- TB_Main-->";
}
echo $ContentLink;
}
像这样吐出html:
<ul id="TB_UL">
<li class="TB_Level1"><a class="TB_Link" href="#Test">Test</a></li>
<li class="TB_Level2"><a class="TB_Link" href="#More Test">More Test</a></li>
</ul>
#Test 和 #More 测试是:<h1>Test</h1>
和<h2>More Test</h2>
有什么想法吗?