<?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;
?>
DOMElement::removeAttribute
(PHP 5)
DOMElement::removeAttribute — Elimina un atributo
Descripción
bool DOMElement::removeAttribute
( string $name
)
Elimina el atributo llamado name del elemento.
Parámetros
- name
-
El nombre del atributo.
Valores devueltos
Devuelve TRUE en caso de éxito o FALSE en caso de error.
Errores/Excepciones
- DOM_NO_MODIFICATION_ALLOWED_ERR
-
Lanzado si el nodo es de sólo lectura.
Ver también
- DOMElement::hasAttribute() - Comprueba si existe un atributo
- DOMElement::getAttribute() - Devuelve el valor de un atributo
- DOMElement::setAttribute() - Añade un nuevo atributo
Rakesh Verma - rakeshnsony at gmail dot com
11-Nov-2010 07:38
