Example #1
0
 /**
  * Add ids to headings tags based on their content
  *
  * @static
  * @param string $html html string
  * @param string $headingSelector CSS selector
  * @return mixed modified
  */
 public static function addHeadingsId($html, $headingSelector = 'h1, h2, h3, h4, h5, h6', $addLink = false)
 {
     if (!$html) {
         return '';
     }
     $document = new \DOMDocument();
     $document->loadHTML('<?xml encoding="UTF-8">' . $html);
     $xpath = new \DOMXPath($document);
     // Search heading tags
     $ids = array();
     foreach ($xpath->query(CssSelector::toXPath($headingSelector)) as $node) {
         // If they don't have an id, find an unique one
         if (!$node->hasAttribute('id')) {
             $id = Inflector::urlize($node->textContent);
             if (array_key_exists($id, $ids)) {
                 $ids[$id] += 1;
                 $id .= '-' . $ids[$id];
             } else {
                 $ids[$id] = 1;
             }
             $node->setAttribute('id', $id);
         }
         if ($addLink) {
             $link = $document->createElement('a', '#');
             $link->setAttribute('href', '#' . $node->getAttribute('id'));
             $link->setAttribute('class', 'anchor');
             $node->appendChild($link);
         }
     }
     // Remove \DomDocument's extra tags (doctype, html, body). Yeah, that's (a bit) ugly.
     return preg_replace('#.*<html><body>(.*)</body></html>.*#is', '\\1', $document->saveHTML());
 }
Example #2
0
 protected function getId($text)
 {
     return $this->fixId(Inflector::urlize($text));
 }