Copyright (c) 2013-2017, Michael Uno; Licensed under MIT */ class AdminPageFramework_TableOfContents { public function __construct($sHTML, $iDepth = 4, $sTitle = '') { $this->sTitle = $sTitle; $this->sHTML = $sHTML; $this->iDepth = $iDepth; } public function get() { return $this->getTOC() . $this->getContents(); } public function getContents() { return $this->sHTML; } public function getTOC() { $iDepth = $this->iDepth; $this->sHTML = preg_replace_callback('/]*>.*?<\/h[2-' . $iDepth . ']>/i', array($this, '_replyToInsertNamedElement'), $this->sHTML); $_aOutput = array(); foreach ($this->_aMatches as $_iIndex => $_sMatch) { $_sMatch = strip_tags($_sMatch, '

'); $_sMatch = preg_replace('//', '
  • ', $_sMatch); $_sMatch = preg_replace('/<\/h[1-' . $iDepth . ']>/', '
  • ', $_sMatch); $_aOutput[] = $_sMatch; } $this->sTitle = $this->sTitle ? '

    ' . $this->sTitle . '

    ' : ''; return '
    ' . $this->sTitle . '
      ' . implode(PHP_EOL, $_aOutput) . '
    ' . '
    '; } protected $_aMatches = array(); public function _replyToInsertNamedElement($aMatches) { static $_icount = - 1; $_icount++; $this->_aMatches[] = $aMatches[0]; return "" . PHP_EOL . $aMatches[0]; } }