0

How do I access PHP's replaceChild method? I need to find and replace all instances of a specific element (in this example a anchor elements). I've been at this for hours already and I can't find it any where. One server is running PHP 5.6.2 and another 5.5.0.

$row1 = '<div><p>A test!</p><a>aaa</a><p>A test!</p><a>bbb</a></div>';

$dom = new DOMDocument();
$dom->loadXML($row1);

$xpath = new DOMXPath($dom);
$entries = $xpath->query('a');

foreach ($entries as $entry)
{
 echo '<div><pre>'; print_r($entry); echo '</pre></div>';//Nope
 echo '<div><pre>'; print_r($entry->parentNode); echo '</pre></div>';//More Nope
}

An example of what this returns:

DOMElement Object
(
    [tagName] => a
    [schemaTypeInfo] => 
    [nodeName] => a
    [nodeValue] => aaa
    [nodeType] => 1
    [parentNode] => (object value omitted)
    [childNodes] => (object value omitted)
    [firstChild] => (object value omitted)
    [lastChild] => (object value omitted)
    [previousSibling] => (object value omitted)
    [nextSibling] => (object value omitted)
    [attributes] => (object value omitted)
    [ownerDocument] => (object value omitted)
    [namespaceURI] => 
    [prefix] => 
    [localName] => a
    [baseURI] => //Private
    [textContent] => aaa
)

Regardless of whether I'm accessing the element directly or indirectly print_r never reveals PHP's replaceChild method! Obviously I read the manual. No other questions on here have any where near a working applicable answer. Suggestions please?

Update: I've abandoned the specific approach that required this due to an utter lack of a working answer any where and this isn't a duplicate if there is no working answer (hint: there is not, drive-by-mod should be banned). For future purposes if I have not accepted an answer then I can not guarantee that it worked in any way though I will comment if I have time to start testing it. I'll leave this here for the benefit of others. As for my project I essentially used a bit of explode() in PHP to get the job done which is lame but not as lame as php.net lacking working examples or people posting not-working-answers elsewhere.

John
  • 1
  • 13
  • 98
  • 177
  • I may be misunderstanding. print_r() doesn't *reveal* methods does it? DOMElement inherits replaceChild() from DOMNode so you should be able to access it with `$entry->replaceChild(...)` – Mike B Apr 12 '17 at 21:53
  • Possible duplicate of [How can I use XPath and DOM to replace a node/element in php?](http://stackoverflow.com/questions/4614434/how-can-i-use-xpath-and-dom-to-replace-a-node-element-in-php) – Alister Bulman Apr 12 '17 at 21:54
  • @AlisterBulman The accepted answer does not work, remove the you-didn't-check-duplicate status. – John Apr 12 '17 at 22:00
  • @MikeB `Undefined property: DOMElement::$replaceChild`. I have not found any reliable documentation about how to access `DOMNode`, suggestions please? – John Apr 12 '17 at 22:03
  • replaceChild is a method not a property. Are you calling it with `$entry->replaceChild` or `$entry->replaceChild()` – Mike B Apr 13 '17 at 13:27
  • @MikeB I use `print_r($entries->replaceChild);` to figure out what I have access to usually and naturally documentation though there are no examples on php.net and every example answer on SO doesn't work at all so I don't have anything to duplicate. :-\ – John Apr 13 '17 at 18:35
  • I'm just not understanding what you're expecting with that. If you have a class Car with method openDoor() then what would you ever expect `$car->openDoor` to do? There is no openDoor property.. it's a method. – Mike B Apr 13 '17 at 19:17

0 Answers0