public static function fixText(string $text, int $delta = 0, bool $relocateOrphans = false) : TextWithHeader
    {
        $headers = self::find($text);
        $headers->fix($delta, $relocateOrphans);

        foreach ($headers->getRecursiveReverseIterator() as $header) {
            $realLevel = $header->getRealLevel();
            $id = $header->getId();

            // Use a substring to ensure that if the string length change, we do
            // not squash existing text around or leave cruft in text.
            $substring = \strtr(
                \substr($text, $header->offset, $header->length),
                [
                    '<h'.$header->userLevel.'>' => '<h'.$realLevel.' id="'.$id.'">',
                    '</h'.$header->userLevel.'>' => '</h'.$realLevel.'>'
                ]
            );

            $text = substr_replace($text, $substring, $header->offset, $header->length);
        }

        return new TextWithHeader($headers, $text);
    }
public static function fixText(string $text, int $delta = 0, bool $relocateOrphans = false) : TextWithHeader
    {
        $headers = self::find($text);
        $headers->fix($delta, $relocateOrphans);

        foreach ($headers->getRecursiveReverseIterator() as $header) {
            $realLevel = $header->getRealLevel();
            $id = $header->getId();

            // Use a substring to ensure that if the string length change, we do
            // not squash existing text around or leave cruft in text.
            $substring = \strtr(
                \substr($text, $header->offset, $header->length),
                [
                    '<h'.$header->userLevel.'>' => '<h'.$realLevel.' id="'.$id.'">',
                    '</h'.$header->userLevel.'>' => '</h'.$realLevel.'>'
                ]
            );

            $text = substr_replace($text, $substring, $header->offset, $header->length);
        }

        return new TextWithHeader($headers, $text);
    }
public static function fixText ( string $ text , int $ delta = 0 , bool $ relocateOrphans = false ) : TextWithHeader { $ headers = self :: find ( $ text ) ; $ headers -> fix ( $ delta , $ relocateOrphans ) ; foreach ( $ headers -> getRecursiveReverseIterator ( ) as $ header ) { $ realLevel = $ header -> getRealLevel ( ) ; $ id = $ header -> getId ( ) ; // Use a substring to ensure that if the string length change, we do // not squash existing text around or leave cruft in text. $ substring = \ strtr ( \ substr ( $ text , $ header -> offset , $ header -> length ) , [ '<h' . $ header -> userLevel . '>' => '<h' . $ realLevel . ' id="' . $ id . '">' , '</h' . $ header -> userLevel . '>' => '</h' . $ realLevel . '>' ] ) ; $ text = substr_replace ( $ text , $ substring , $ header -> offset , $ header -> length ) ; } return new TextWithHeader ( $ headers , $ text ) ; }
Find all headers in text and fix semantical hierarchy

@param string $text
Original text to fix
@param int $delta
Per default 0. If you need your headings to semantically start with h3
intead of h1 for example, set 2 here, if you with to start with h2, set
1, etc..
@param bool $relocateOrphans
If set to true, when a heading is alone (has no siblings at the same level)
exists then it's reattached to its parent in the tree.

@return string
Fixed text
Find all headers in text and fix semantical hierarchy