downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

DOMElement::removeAttributeNode> <DOMElement::hasAttributeNS
[edit] Last updated: Fri, 10 Feb 2012

view this page in

DOMElement::removeAttribute

(PHP 5)

DOMElement::removeAttributeRemoves attribute

Description

bool DOMElement::removeAttribute ( string $name )

Removes attribute named name from the element.

Parameters

name

The name of the attribute.

Return Values

Returns TRUE on success or FALSE on failure.

Errors/Exceptions

DOM_NO_MODIFICATION_ALLOWED_ERR

Raised if the node is readonly.

See Also



add a note add a note User Contributed Notes DOMElement::removeAttribute
Rakesh Verma - rakeshnsony at gmail dot com 10-Nov-2010 11:38
<?php

//Store your html into $html variable.

$html="<html>
<head>
<title>Rakesh Verma</title>
</head>

<body>
    <a href='http://example.com'>Example</a>
    <a href='http://google.com'>Google</a>
    <a href='http://www.yahoo.com'>Yahoo</a>
</body>

</html>"
;

$dom = new DOMDocument();
$dom->loadHTML($html);

//Evaluate Anchor tag in HTML
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");

for (
$i = 0; $i < $hrefs->length; $i++) {
       
$href = $hrefs->item($i);
       
$url = $href->getAttribute('href');

       
//remove and set target attribute       
       
$href->removeAttribute('target');
       
$href->setAttribute("target", "_blank");

       
$newURL=$url.".au";

       
//remove and set href attribute       
       
$href->removeAttribute('href');
       
$href->setAttribute("href", $newURL);
}

// save html
$html=$dom->saveHTML();

echo
$html;

?>

 
show source | credits | stats | sitemap | contact | advertising | mirror sites