tags:

views:

490

answers:

3

Hi folks

Current image looks like

<img src="images/example.jpg" />

Now if img src do not have the alt="", the code itself will replace the image like

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

How can be done with php?

+4  A: 

The quickest solution, assuming images always follow that format, is this:

$search = '/<img src="([^"]+)" \/>/';
$replace = '<img src="$1" alt="" />';
$code = preg_replace( $search, $replace, $code );

However, I would question your motives. Adding a blank alt tag is pretty pointless.

DisgruntledGoat
but an alt tag is required per html standard—even if it's empty
knittl
+1 to knittl for being correct, but it's only a question of validation: every browser out there will assume an empty string as the alt text if there's none given.
Matthew Scharley
+1 ... a bit static, but elegant ... ;)
back2dos
I am aware the alt tag is "required" per the spec, but validation is not the be-all-and-end-all. I'm really saying a blank one is as bad as no alt tag, assuming the image is meaningful. (The spec does say blank alt tags are fine on some decorative images, but I rarely see a use case for this as CSS takes that role.)
DisgruntledGoat
thanks DisgruntledGoat, code working fine. How about if the image has a height and weight? How to make the code will keep current height and weight but just change the alt tag only.
bob
bob, if you can't be sure of the exact structure of the image tags it is best to use proper XML processing than try to achieve this with regex, as the regex will become very complex very quickly as you try to cover all of the possibilities.
Ben James
@DisgruntledGoat: true, it isn't the be-all-end-all. But I like to keep my sites as valid as possible, it's generally a good first step in debugging problems. The less warnings I have to sift through to get to the actual problems the better. I'd question the motives behind needing to *fix* tags though. Fix the source of the problem, don't try to filter things after they've been output.
Matthew Scharley
A: 

if it is valid XML, you can use about any decent XML parser (SimpleXML for example), unmarshall the source, traverse the tree looking for image tags, try lookup the attribute and set it if it does not exist, and the remarshall the xml ...

should be a 10-liner or so ...

good luck ... :)

back2dos
+1  A: 

Here's an example of doing this with SimpleXML.

<?php

$html = '<html>
    <body>
        <img src="foo.jpg" />
    </body>
</html>';

$xml = new SimpleXMLElement($html);

foreach ($xml->xpath('//img') as $img) {
    if (!array_key_exists('alt', $img->attributes())) {
        $img->addAttribute('alt', '');
    }
}

echo $xml->asXML();

The output will be:

<?xml version="1.0"?>
<html>
    <body>
        <img src="foo.jpg" alt=""/>
    </body>
</html>
Ben James
I tested and working.. learned something new today. Thanks Ben James. The xml version="1.0" possible to remove?
bob
I'm not sure if you can prevent SimpleXML from adding that, as it will only output valid XML. So it could be a problem if you're not planning to use XHTML. However, you could easily write your own filter to remove it.
Ben James
Of course, if you are using XHTML, there is no need to remove it.
Ben James
ok thanks.. http://us3.php.net/manual/en/xsltprocessor.transformtoxml.php#80887
bob