Jump to content

Recommended Posts

I am sure my use of the word "persistent" is wrong.  Let me explain.  Consider the following code.  If I create a single element and then append it multiple times to another element, will each time it is appended be its own unique object, or will they all be the same object?  Thanks


$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);

$widget1s=[];
foreach( $xpath->query( "//img[contains(concat(' ', normalize-space(@class), ' '), ' widget1 ')]") as $img) {
    if( $id=$img->getAttribute('data-id')) {
        if(!in_array($id,$widget1s)) {
            $widget1s[]=$id;
        }
        $elem = $doc->createElement('span');                    
        $class=$doc->createAttribute('class');
        $class->value='widget1 w1'.$id;
        $elem->appendChild($class);

        $elem_value = $doc->createElement('span');                    
        $class=$doc->createAttribute('class');
        $class->value='value';
        $elem_value->appendChild($class);
        $elem->appendChild($elem_value);

        $elem_units = $doc->createElement('span');                    
        $class=$doc->createAttribute('class');
        $class->value='units';
        $elem_units->appendChild($class);
        $elem->appendChild($elem_units);

        $data_id=$doc->createAttribute('data-id');
        $data_id->value=$id;
        $elem->appendChild($data_id);

        $img->parentNode->replaceChild($elem,$img);
    }
}
$widgets=[];
foreach( $xpath->query( "//img[contains(concat(' ', normalize-space(@class), ' '), ' widget2 ')]") as $img) {
    if( $id=$img->getAttribute('data-id')) {
        if(!in_array($id,$widget2s)) {
            $widget2s[]=$id;
        }
        $elem = $doc->createElement('div');                    
        $class=$doc->createAttribute('class');
        $class->value='widget2 w2'.$id;
        $elem->appendChild($class);
        $data_id=$doc->createAttribute('data-id');
        $data_id->value=$id;
        $elem->appendChild($data_id);
        $img->parentNode->replaceChild($elem,$img);
    }
}
Link to comment
https://forums.phpfreaks.com/topic/301659-are-domdocument-objects-persistent/
Share on other sites

I'm not really sure what your asking. So long as you call createElement each iteration you'll be creating new unique element objects. If you tried to just re-use some element, such as:

$elem = $doc->createElement('div');
foreach ($xpath->query('//something') as $something){
  $something->appendChild($elem);
}
Then you'd end up with a document with only a single div appended to the last $something as each call to appendChild would just move it rather than duplicate it.

 

If you're familiar at all with how DOM manipulation works in Javascript, then it works the same way in PHP.

I'm not really sure what your asking. So long as you call createElement each iteration you'll be creating new unique element objects. If you tried to just re-use some element, such as:

 

Then you'd end up with a document with only a single div appended to the last $something as each call to appendChild would just move it rather than duplicate it.

If you're familiar at all with how DOM manipulation works in Javascript, then it works the same way in PHP.

 

 

Thanks Kicken,

 

Was rushing out the door, and apologize for my cryptic post.  Yes, I was kind of asking whether DOM manipulation is similar to JavaScript.

 

For instance, would the following create <div><span>foo</span><span>bar</span></div> or <div><span>bar</span><span>bar</span></div>? Based on your response, I would expect the later.

$div = $doc->createElement('div');
$span = $doc->createElement('span');                    
$span ->value='foo';
$div ->appendChild($span );
$span ->value='bar';
$div ->appendChild($span );

For instance, would the following create <div><span>foo</span><span>bar</span></div> or <div><span>bar</span><span>bar</span></div>?

Neither, it would create: <div><span>bar</span></div> (->value should be ->nodeValue).

 

The second appendChild essentially does nothing. $span can only exist at one place in the document. The first appendChild would place it as the first child of $div. The second appendChild would move it to the "second child of div" position, but that would remove it as the first-child meaning it's still just the first-child in the end.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.