Change Tag Attribute Value with PHP Domdocument

Change tag attribute value with PHP DOMDocument

$dom = new DOMDocument();
$dom->loadHTML('<a href="http://foo.bar/">Click here</a>');

foreach ($dom->getElementsByTagName('a') as $item) {

$item->setAttribute('href', 'http://google.com/');
echo $dom->saveHTML();
exit;
}

PHP DOMDocument getting Attribute of Tag

The example you provided should not generate an error. I tested it and $linkthumb contained the string "example text string" as expected

Ensure the media namespace is defined in the returned XML otherwise DOMDocument will error out.

If you are getting a specific error, please edit your post to include it

Edit:

Try the following code:

$xmldoc = new DOMDocument();
$xmldoc->load('api response address');
foreach ($xmldoc->getElementsByTagName('item') as $feeditem) {
$nodes = $feeditem->getElementsByTagName('file');
$linkthumb = $nodes->item(0)->getAttribute('data');
echo $linkthumb;
}

You may also want to look at SimpleXML and Xpath as it makes reading XML much easier than DOMDocument.

How to user DOMElement to replace attribute value

PHP is different from jQuery in a lot of very significant ways, but to answer your question:

$domelement->setAttribute("href", "http://www.google.se");

You might want to consult the documentation.

PHP DOMDocument remove tag attribute

Probably xml:lang="en" is not corrected value of attribute in html. Change these lines:

$dom->loadHTML($content, LIBXML_HTML_NOIMPLIED);
//code...
echo $dom->saveHTML($dom->documentElement);

to

$dom->loadXML($content, LIBXML_HTML_NOIMPLIED);
//code...
echo $dom->saveXML($dom->documentElement);

Modify XML attribute PHP DOM

Not sure what you want to do exactly, but the general idea is :

  • You must instanciate DOMDocument
  • and load your XML strings with it : DOMDocument::loadXML
  • Then, you must instanciate DOMXpath on that document
  • And use it to query the document : DOMXPath::query
  • One you have found the node that interests you, you can manipulate it

    • for example, you can set the value of an attribute : DOMElement::setAttribute


Here, for example, you could use some thing like this :

$str = <<<XML
<collections id="My Collections">
<category id="my category">
<record id="my record">
<title>Some Info</title>
</record>
</category>
</collections>
XML;

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

$xpath = new DOMXPath($dom);
$elements = $xpath->query('//record[@id="my record"]');
if ($elements->length >= 1) {
$element = $elements->item(0);
$element->setAttribute('id', "glop !");
}
echo '<pre>' . htmlspecialchars($dom->saveXML()) . '</pre>';


This will replace the id attribute my record, on the node that's identified by it, by "glop !", and you'd get the following XML as output :

<?xml version="1.0"?>
<collections id="My Collections">
<category id="my category">
<record id="glop !">
<title>Some Info</title>
</record>
</category>
</collections>

PHP DOMDocument: Get attribute value from id

Try this :

$data->getAttribute('value'); 

PHP: DomElement->getAttribute

$attrs = array();
for ($i = 0; $i < $data->attributes->length; ++$i){
$node = $data->attributes->item($i);
$attrs[$node->nodeName] = $node->nodeValue;
}
var_dump($attrs);

In PHP, how can I use the DOMDocument class to replace the src= attribute of an IMG tag?

This is what I've come up with. It uses the PHP DOM API to create a tiny HTML document, then saves the XML for just the IMG element.

function replace_img_src($original_img_tag, $new_src_url) {
$doc = new DOMDocument();
$doc->loadHTML($original_img_tag);

$tags = $doc->getElementsByTagName('img');
if(count($tags) > 0)
{
$tag = $tags->item(0);
$tag->setAttribute('src', $new_src_url);
return $doc->saveHTML($tag);
}

return false;
}

Note: In versions of PHP before 5.3.6, $doc->saveHTML($tag) can be changed to $doc->saveXML($tag).

DOMDocument add attribute to root tag

When you output the HTML, select a specific node rather than the whole document:

<?php

$content = '<h1 class="no-margin">Lorem</h1>';

$dom = new \DOMDocument();
$dom->loadHTML($content);

$node = $dom->getElementsByTagName('h1')->item(0);
$node->setAttribute('data-custom','true');

print $dom->saveHTML($node);
// <h1 class="no-margin" data-custom="true">Lorem</h1>

Alternatively, as it's well-formed, treat the content as XML to avoid extra HTML tags being added:

<?php

$content = '<h1 class="no-margin">Lorem</h1>';

$dom = new \DOMDocument();
$dom->loadXML($content);

$dom->documentElement->setAttribute('data-custom','true');

print $dom->saveXML($dom->documentElement);
// <h1 class="no-margin" data-custom="true">Lorem</h1>


Related Topics



Leave a reply



Submit