tag attribute value change using PHP

by Francis 2013-09-17 17:55:08

tag attribute value change using PHP
Here, for example to change the image alt value...


$html_content = tag_attributes($html_content,$newaltag,$imgname);

// Imgage attributes replace
function tag_attributes($content,$altag,$imgname)
{
$dom=new DOMDocument();
@$dom->loadHTML($content);
$imgs = $dom->getElementsByTagName("img");
foreach($imgs as $img)
{
$src = $img->getAttribute('src');
if ($src == $imgname)
{
$alt = $altag;
}
else
{
$alt = '';
}
$img->setAttribute('alt',$alt);
}
$content = $dom->saveHTML();
$content = preg_replace('~<(?:!DOCTYPE|/?(?:html|head|body))[^>]*>\s*~i', '', $content);
return $content;
}
898
like
0
dislike
0
mail
flag

You must LOGIN to add comments