-1

I wrote this function but it doesn't seem to be saving the $dom. If I check the attribute after I set it, it is correct, but it just doesn't make it to the final save.

function tj_add_alts( $content ) {
$dom = new DOMDocument();
$dom->loadHTML($content);

foreach ($dom->getElementsByTagName( 'img' ) as $node) {
    if ( trim( $node->getAttribute( 'alt' ) ) == "" ) {
        $img = $node->getAttribute( 'src' );
        $file_name = pathinfo($img, PATHINFO_FILENAME);
        $name = preg_replace( '/[^A-Za-z0-9 ]/', ' ', $file_name);
        $name = preg_replace( '/\s{2,}/', ' ', $name);
        $node->setAttribute( 'alt', $name );
    }
}

$content = $dom->saveHTML();

return $content;
}

UPDATE: It looks like the problem is that DomDocument automatically adds the doctype declaration and <body> and <head> tags, which I don't want. Is there a way to get ride of those?

  • [DOMDocument::saveHTML](http://www.php.net/manual/en/domdocument.savehtml.php)'s 2nd argument allows you to output a subset of the document, just use it to get the HTML portion you want. If you add an example of your HTML we might come up with a solution. – Rolando Isidoro Jul 01 '13 at 16:59

2 Answers2

0

Did you mean to use the saveHTMLFile method?

See: http://www.php.net/manual/en/domdocument.savehtmlfile.php

Rob W
  • 9,134
  • 1
  • 30
  • 50
0

I ran the following through your function to test it:

$content = <<<EOTAAA
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<h1>Image:</h1>
<img src="images/betsy.jpg" alt="Betsy eating an apple"/>
<img src="images/doug.jpg" alt=""/>
</body>
</html>

EOTAAA;

Then I echoed the value of $content which was returned from the function. The result was that your function works great. Nothing happed with the image tag for betsy.jpg because the function only modifies the HTML when it finds an empty alt attribute. However the image tag for doug.jpg was as follows:

<img src="images/doug.jpg" alt="doug">

I'm guessing that's exactly what you want it to do, so if there's a problem, it has nothing to do with the code you posted.

Expedito
  • 7,771
  • 5
  • 30
  • 43
  • I have found where the problem is. domdocument automatically adds the `doctype` declaration and `html and body` tags. Do you know of a way to suppress that? – TimothyBJacobs Jul 01 '13 at 16:54
  • That issue was brought up in another SO question: http://stackoverflow.com/questions/5444510/php-domdocument-adds-html-headers-with-doctype-declaration. Hope that helps. – Expedito Jul 01 '13 at 21:57