Loading...   


function buildMenu($aMenu) {
		$sHMTL .= '<ul>';
		foreach($aMenu as $mKey => $mValue)
		{
			if(is_array($mValue))
			{
				$sHMTL .= sprintf(
					'<li>%s %s</li>',
					$mKey,
					buildMenu($mValue)
				);
			}
			else
			{
				$sHMTL .= sprintf(
					'<li><a href="%s">%s</a></li>',
					$mValue,
					$mKey
				);
			}
		}
		$sHMTL .= '</ul>';
		return $sHMTL;
	}  
function SpitTOC($TOC){
		$iter = 0; $stage = 0;
		while($TOC[$iter]){
			$Depth = $TOC[$iter][1]; /* Get current item Depth in Iteration... */
			if(isset($TOC[($iter + 1)][1])){ $F_Depth = $TOC[($iter + 1)][1]; } /* Depth of the next item, after the current */
			$ItemName = $TOC[$iter][0];
			/* Break out ToC Depths and declare them into stages...
				This will automatically take in the Header parsings of the page and auto format them in a nested tree and return an array
					for the list output...
				This could probably be done cleaner, if you know how to make it prettier let me know...
			*/
			if($stage == 5){
				$Lev_6 = $ItemName; #::: Set Level 6 Link Name and Key
				if(!is_array($TOCMenu[$Lev_1][$Lev_2][$Lev_3][$Lev_4][$Lev_5])){  $TOCMenu[$Lev_1][$Lev_2][$Lev_3][$Lev_4][$Lev_5] = array(); }
				$TOCMenu[$Lev_1][$Lev_2][$Lev_3][$Lev_4][$Lev_5][$Lev_6] = '#' . $ItemName; /* Push Items... */
				if($F_Depth > $Depth){ $stage++; }
				else if($F_Depth < $Depth){ $stage = 0; }
			}
			else if($stage == 4){
				$Lev_5 = $ItemName; #::: Set Level 5 Link Name and Key
				if(!is_array($TOCMenu[$Lev_1][$Lev_2][$Lev_3][$Lev_4])){  $TOCMenu[$Lev_1][$Lev_2][$Lev_3][$Lev_4] = array(); }
				$TOCMenu[$Lev_1][$Lev_2][$Lev_3][$Lev_4][$Lev_5] = '#' . $ItemName; /* Push Items... */
				if($F_Depth > $Depth){ $stage++; }
				else if($F_Depth < $Depth){ $stage = 0; }
			}
			else if($stage == 3){
				$Lev_4 = $ItemName; #::: Set Level 4 Link Name and Key
				if(!is_array($TOCMenu[$Lev_1][$Lev_2][$Lev_3])){  $TOCMenu[$Lev_1][$Lev_2][$Lev_3] = array(); }
				$TOCMenu[$Lev_1][$Lev_2][$Lev_3][$Lev_4] = '#' . $ItemName; /* Push Items... */
				if($F_Depth > $Depth){ $stage++; }
				else if($F_Depth < $Depth){ $stage = 0; }
			}
			else if($stage == 2){
				$Lev_3 = $ItemName; #::: Set Level 3 Link Name and Key
				if(!is_array($TOCMenu[$Lev_1][$Lev_2])){  $TOCMenu[$Lev_1][$Lev_2] = array(); }
				$TOCMenu[$Lev_1][$Lev_2][$Lev_3] = '#' . $ItemName; /* Push Items... */
				if($F_Depth > $Depth){ $stage++; }
				else if($F_Depth < $Depth){ $stage = 0; }
			}
			else if($stage == 1){
				$Lev_2 = $ItemName; #::: Set Level 2 Link Name and Key
				if(!is_array($TOCMenu[$Lev_1])){  $TOCMenu[$Lev_1] = array(); }
				$TOCMenu[$Lev_1][$Lev_2] = '#' . $ItemName; /* Push Items... */
				if($F_Depth > $Depth){ $stage++; }
				else if($F_Depth < $Depth){ $stage = 0; }
			}
			else if($stage == 0){ 
				$Lev_2 = $Lev_3 = $Lev_4 = $Lev_5 = $Lev_6 = ''; /* Clear out the stage variables... */
				$Lev_1 = $ItemName;
				$TOCMenu[$Lev_1] = '#' . $ItemName; /* Push Items... */
				if($F_Depth > $Depth){ $stage++; }
				else if($F_Depth < $Depth){ $stage = 0; }
			}
			# echo $TOC[$iter][0] . ' ' . $TOC[$iter][1] . '<br>';
			$iter++;
		}
		# echo '<hr>';
		return $TOCMenu;
	}

/* Build Table of Contents... */
		$data = explode("\n", $Content); $TOC = array(); $n = 0;
		foreach($data as $line) {
			if(preg_match('/<h1>|<h2>|<h3>|<h4>|<h5>|<h6>/i', $line)){
				preg_match_all("/<h1>(.*?)<\/h1>/", $line, $matches);  foreach ($matches[1] as $v => $k){ $TOC[$n] = array(strip_tags($k), 1); $n++; }
				preg_match_all("/<h2>(.*?)<\/h2>/", $line, $matches);  foreach ($matches[1] as $v => $k){ $TOC[$n] = array(strip_tags($k), 2); $n++; }
				preg_match_all("/<h3>(.*?)<\/h3>/", $line, $matches);  foreach ($matches[1] as $v => $k){ $TOC[$n] = array(strip_tags($k), 3); $n++; }
				preg_match_all("/<h4>(.*?)<\/h4>/", $line, $matches);  foreach ($matches[1] as $v => $k){ $TOC[$n] = array(strip_tags($k), 4); $n++; }
				preg_match_all("/<h5>(.*?)<\/h5>/", $line, $matches);  foreach ($matches[1] as $v => $k){ $TOC[$n] = array(strip_tags($k), 5); $n++; }
				preg_match_all("/<h6>(.*?)<\/h6>/", $line, $matches);  foreach ($matches[1] as $v => $k){ $TOC[$n] = array(strip_tags($k), 6); $n++; }
			}
		} 
		if($_SESSION['username'] == "akkadius"){
			echo '<br><div class="alert alert-info"> <h3>Table of Contents (Testing)</h3><hr>';
			
			$TOCMenu = array();
			$iter = 0;
			$TOCMenu = SpitTOC($TOC);
			
			echo buildMenu($TOCMenu);
			echo '</div>';
			
			echo '<pre>';
			echo print_r($TOCMenu);
			echo '</pre>';
			

		}
		/* End TOC */

Raw Paste Data