note 113995 added to domelement.getattribute
| From: | rakeshnsony at gmail dot com | Date: | Fri, 27 Dec 2013 09:31:51 +0000 |
| Subject: | note 113995 added to domelement.getattribute | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-198036@lists.php.net to get a copy of this message | ||
<?php
//Store your html into $html variable.
$html="
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<a href='http://example.com'>Example</a><br>
<a href='http://google.com'>Google</a><br>
<a href='http://www.yahoo.com'>Yahoo</a><br>
</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."/newurl";
//remove and set href attribute
$href->removeAttribute('href');
$href->setAttribute("href", $newURL);
}
// save html
$html=$dom->saveHTML();
echo $html;
?>
----
Server IP: 72.52.91.14
Probable Submitter: 72.52.91.14
----
Manual Page -- http://php.net/manual/en/domelement.getattribute.php
Edit -- https://master.php.net/note/edit/113995
Del: integrated -- https://master.php.net/note/delete/113995/integrated
Del: useless -- https://master.php.net/note/delete/113995/useless
Del: bad code -- https://master.php.net/note/delete/113995/bad+code
Del: spam -- https://master.php.net/note/delete/113995/spam
Del: non-english -- https://master.php.net/note/delete/113995/non-english
Del: in docs -- https://master.php.net/note/delete/113995/in+docs
Del: other reasons-- https://master.php.net/note/delete/113995
Reject -- https://master.php.net/note/reject/113995
Search -- https://master.php.net/manage/user-notes.php