Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8679765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:03:47+00:00 2026-06-12T21:03:47+00:00

Possible Duplicate: How to parse and process HTML with PHP? I am looking for

  • 0

Possible Duplicate:
How to parse and process HTML with PHP?

I am looking for a php function that decreases html hx (h1, h2, h3, …, h6) tags by one.

  • h1 becomes h2
  • h2 becomes h3 and so on
  • …
  • h6 gets replaced by ‘ ‘

do you know such a function?

This is how I started stripping the h6 tags:

$string = preg_replace('#<(?:/)?\s*h6\s*>#', ' ', $string);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:03:48+00:00Added an answer on June 12, 2026 at 9:03 pm

    Here’s one for DOM, which iterates trough all mappings and then replace tags or copies children.

    <?php
    
    // New tag mappings:
    //     null => extract childs and push them into parent contrainer
    // Make sure that they are in this order, otherwise they would match wrongly
    // between each another
    $mapping = array(
        'h6' => null,
        'h5' => 'h6',
        'h4' => 'h5',
        'h3' => 'h4',
        'h2' => 'h3',
        'h1' => 'h2'
    );
    
    // Load document
    $xml = new DOMDocument();
    $xml->loadHTMLFile('http://stackoverflow.com/questions/12883009/php-code-to-decrease-html-hx-tags') or die('Failed to load');
    
    $xPath = new DOMXPath( $xml);
    
    foreach( $mapping as $original => $new){
        // Load nodes
        $nodes = $xPath->query( '//' . $original);
    
        // This is a critical error and should NEVER happen
        if( $nodes === false){
            die( 'Malformed expression: //' . $original);
        }
    
        echo $original . ' has nodes: ' . $nodes->length . "\n";
    
        // Process each node
        foreach( $nodes as $node){
            if( $new == null){
                // Append all the childs before self and remove self afterwards
                foreach( $node->childNodes as $child){
                    $node->parentNode->insertBefore( $child->cloneNode( true), $node);
                }
                $node->parentNode->removeChild( $node);
    
            } else {
                // Create new empty node and push all childrens to it
                $newNode = $xml->createElement( $new);
                foreach( $node->childNodes as $child){
                    $newNode->appendChild( $child);
                }
                $node->parentNode->replaceChild( $newNode, $node);
            }
        }
    }
    
    echo $xml->saveHTML();
    

    You may also do some xPath optimalizations like using //* or //h3|//h2 and checking DOMElement::tagName, but I wanted this to be straight forward.


    Edit: Solution that goes trough nodes only once and doesn’t care about order:

    <?php
    // The beginning (everything to the first foreach loop) remains the same
    // Load nodes
    $nodes = $xPath->query( '//*');
    
    // This is a critical error and should NEVER happen
    if( $nodes === false){
        die( 'Malformed expression: //' . $original);
    }
    
    // Process each node
    foreach( $nodes as $node){
        // Check correct $node class
        if( !($node instanceof DOMElement)){
            continue;
        }
        $tagName = $node->tagName;  
    
        // Do we have a mapping?
        if( !array_key_exists( $tagName, $mapping)){
            continue;
        }
        $new = $mapping[$tagName];
        echo 'Has element: ' . $tagName . ' => ' . $new . "\n";
    
        if( $new == null){
            // Append all the childs before self and remove self afterwards
            foreach( $node->childNodes as $child){
                $node->parentNode->insertBefore( $child->cloneNode( true), $node);
            }
            $node->parentNode->removeChild( $node);
    
        } else {
            // Create new empty node and push all childrens to it
            $newNode = $xml->createElement( $new);
            foreach( $node->childNodes as $child){
                $newNode->appendChild( $child);
            }
            $node->parentNode->replaceChild( $newNode, $node);
        }
    }
    
    echo $xml->saveHTML();
    

    And the last optimalization I can think about is using:

    $xPathQuery = '//' . implode( array_keys($mapping), '|//');
    $nodes = $xPath->query( $xPathQuery);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to parse and process HTML with PHP? I'm looking into HTML
Possible Duplicate: How to parse and process HTML with PHP? I am brand new
Possible Duplicate: How to parse and process HTML with PHP? I have HTML document
Possible Duplicate: How to parse and process HTML with PHP? $content = <p>This is
Possible Duplicate: How to parse and process HTML with PHP? Here is what I'm
Possible Duplicate: How to parse and process HTML with PHP? I need to parse
Possible Duplicate: How to parse and process HTML/XML with PHP? I want to grab
Possible Duplicate: How to parse and process HTML with PHP? I'm not very good
Possible Duplicate: How to parse and process HTML with PHP? I'm trying to use
Possible Duplicate: How to parse and process HTML with PHP? Hi there I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.