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 8604427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:34:15+00:00 2026-06-12T02:34:15+00:00

I am trying to insert nodes in my html string. My goal is to

  • 0

I am trying to insert nodes in my html string.
My goal is to insert an element before each h2 tag.

For that, I am using:

$htmlString = "<h2>some html</h2>";

$DOM = new DOMDocument();
$DOM->loadHTML($htmlString);

$itemTitles = $DOM->getElementsByTagName('h2');

for($i = 0; $i < $itemTitles->length; $i ++)
{
    $helpNavigatorContents[] = $itemTitles->item($i)->nodeValue;
    $textBefore = new DOMNode(
        '<a name="'.$itemTitles->item($i)->nodeValue.'"></a>'
    );
    $itemTitles->item($i)->parentNode->insertBefore(
        $textBefore, 
        $itemTitles->item($i)
    );
}

$htmlString = $DOM->saveHTML($DOM);

And here I have a problem with the $textBefore. When I declare the $textBefore as a DOMText, I can insert the text before the node but when I try this with DOMNode, then I am getting the following error (Demo):

Warning: DOMNode::insertBefore(): Couldn’t fetch DOMNode

  • 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-12T02:34:17+00:00Added an answer on June 12, 2026 at 2:34 am

    The code doesn’t make any sense. DOMNode does not have a constructor. It is not supposed to be created at all. You are supposed to create specific node types through DOMDocument to have them associated with the Document.

    Assuming you want to prepend all the H2 element with an anchor, this is how to do it:

    libxml_use_internal_errors(true);
    $DOM = new DOMDocument();
    $DOM->loadHTML($htmlString);
    $DOM->preserveWhiteSpace = false;
    
    foreach ($DOM->getElementsByTagName('h2') as $h2) {
        $a = $DOM->createElement('a');
        $a->setAttribute('name', $h2->nodeValue);
        $h2->parentNode->insertBefore($a, $h2);
    }
    $DOM->formatOutput = true;
    echo $DOM->saveHTML();
    

    Demo http://codepad.org/N0dPcLwT

    To wrap the H2 elements into the A element, simply do the same and add

    $a->appendChild($h2);
    

    Demo http://codepad.org/w7Hi0Bmz

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to insert a new meta tag within the Head, I'm using a
I'm trying to run through some html and insert some custom tags around every
I'm trying to insert a node in a linked list so that the nodes
I'm trying to insert some HTML at a given point. The XML file has
I'm trying to replace each textNode of the DOM tree using the following function:
I'm trying to get all the DOM nodes that are within a range object,
I am trying to insert xml nodes in this document: </providers> </root> I wrote
I am trying to run the following query to insert a number of nodes
I am creating a module that needs to tag nodes with taxonomy terms when
I'm trying to work on a method that will insert the node passed to

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.