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

  • SEARCH
  • Home
  • 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 8536579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:42:06+00:00 2026-06-11T10:42:06+00:00

Pulling my hair out here… I need to insert a bunch of html into

  • 0

Pulling my hair out here…

I need to insert a bunch of html into another set set of html at two distinct points, using PHP.

$a is returned to me from another class’s function. I need to insert $b into $a, making the $b elements the parent of some of $a’s elements.

<?php
$a = '
<div id="one">
    <div id="two">
        <div id="three">Hi</div>
    </div>
</div> 
';

$b = '
<div id="red">
    <div id="blue"></div>
</div>
<div id="green">
    <div id="yellow">there</div>
</div>
';
?>

Need to end up with:

<?php
$c = '
<div id="one">
    <div id="two">
        <div id="red">
            <div id="blue">
                <div id="three">Hi</div>
            </div>
        </div>
        <div id="green">
            <div id="yellow">there</div>
        </div>
    </div>
</div>
';
?>

Note how $b is inserted into $a, making all of $b a child to “two”. But at the same time, “three” becomes a child of “blue”. “green” and “yellow” maintain their original relationship, but are now nested under “two”.

Real world example….

<?php
$a = '
<div class="ginput_complex ginput_container">
    <span class="ginput_full">
            <input name="input_5" id="input_4_5" type="file" value="" class="medium" tabindex="5">
            <label for="input_4_5" class="ginput_post_image_file">File</label>
    </span>
    <span class="ginput_full ginput_post_image_title">
            <input type="text" name="input_5.1" id="input_4_5_1" value="" tabindex="6">
            <label for="input_4_5_1">Title</label>
    </span>
    <span class="ginput_full ginput_post_image_caption">
            <input type="text" name="input_5.4" id="input_4_5_4" value="" tabindex="7">
            <label for="input_4_5_4">Caption</label>
    </span>
    <span class="ginput_full ginput_post_image_description">
            <input type="text" name="input_5.7" id="input_4_5_7" value="" tabindex="8">
            <label for="input_4_5_7">Description</label>
    </span>
</div>
';


$b =
'
<div class="container">
    <div class="row fileupload-buttonbar">
        <div class="span7">
            <span class="btn btn-success fileinput-button">
            <i class="icon-plus icon-white"></i>

            {{{{{{{{ THIS IS WHERE I MY FILE INPUT SHOULD GO }}}}}}}}}}}

            </span>
            <button type="submit" class="btn btn-primary start">
                <i class="icon-upload icon-white"></i>
                <span>Start upload</span>
            </button>
            <button type="reset" class="btn btn-warning cancel">
                <i class="icon-ban-circle icon-white"></i>
                <span>Cancel upload</span>
            </button>
            <button type="button" class="btn btn-danger delete">
                <i class="icon-trash icon-white"></i>
                <span>Delete</span>
            </button>
        </div>
    </div>
</div>
';

$doc = new DOMDocument;
$doc->loadHTML($a);
$x = new DOMXPath($doc);

?>

I’m using $x->query(‘//*[@id=”input_4_5″]’) to get to the node I want to start working on. And from there, I’ve been using new DOMElement and importNode with variations of appendChild, replaceChild, and insertBefore to try an traverse the DOM tree. But frankly, even if I did traverse it successfully, I can’t figure out how to insert/append the large blocks of code that I’m working with.

  • 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-11T10:42:07+00:00Added an answer on June 11, 2026 at 10:42 am

    I am using the SimpleHTMLDom parser

    Try this:

    <?php
    require_once( 'simple_html_dom.php' );
    
    $a = '
    <div id="one">
        <div id="two">
            <div id="three">Hi</div>
        </div>
    </div> 
    ';
    
    $b = '
    <div id="red">
        <div id="blue"></div>
    </div>
    <div id="green">
        <div id="yellow">there</div>
    </div>
    ';
    
    $a = str_get_html( $a );
    $c = str_get_html( $b );
    
    $c->find( 'div[id=blue]', 0 )->innertext = $a->find( 'div[id=two]', 0 )->innertext;
    $a->find( 'div[id=two]', 0 )->innertext = $c->save();
    
    echo $a;
    ?>
    

    Hope this helps.

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

Sidebar

Related Questions

I'm pulling my hair out over here trying to set up MySQLdb on my
Pulling my hair out again... I need to calculate the difference between two dates
Pulling my hair out here, but I need to convert an int to a
I'm pulling my hair out here. I'm using the DropDownListFor HTML helper in MVC3
I'm pulling my hair out here trying to get CSS to position only a
I'm pulling my hair out here, because I just can't seem to get this
I'm on the verge of pulling my hair out over this. Here I have
I'm pulling my hair out over here and I'm not sure what's causing it.
So, I'm pulling my hair out here, and maybe someone has an insight. I
I'm pulling my hair out here for that I can't understand what is going

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.