Welcome Guest, Not a member yet? Register   Sign In
Identifying images referenced in HTML
#9

[eluser]cwt137[/eluser]
[quote author="SitesByJoe" date="1234249784"]It'd be much easier to get that info with javascript being that PHP doesn't know the DOM. Is that out of the question?

If not, you're gonna have some beefy regex-type work ahead of you.[/quote]

What? PHP doesn't know DOM? http://us2.php.net/manual/en/book.dom.php

Below is an example I put together in a few minutes on how to get every file name contained in the src attibute of a img tag. I don't use PHP's DOM API much so this solution might not be best but it gets the job done.

Code:
<?php

$string = <<&lt;XML
&lt;html>
    &lt;head&gt;
        &lt;title&gt;Test Page&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        <h1>Test For Images</h1>
        <hr />
        <img src="foo.jpg" />
        <div>
            <img src="/some_dir/bar.png" />
        </div>
    &lt;/body&gt;
&lt;/html&gt;
XML;

$doc = new DOMDocument;
$doc->loadHTML($string);
$img_tags = $doc->getElementsByTagName('img');

foreach ($img_tags as $img_tag) {
    echo $img_tag->attributes->getNamedItem("src")->nodeValue . "<br />\n";

}

?&gt;


Messages In This Thread
Identifying images referenced in HTML - by El Forum - 02-09-2009, 03:16 PM
Identifying images referenced in HTML - by El Forum - 02-09-2009, 03:26 PM
Identifying images referenced in HTML - by El Forum - 02-09-2009, 03:28 PM
Identifying images referenced in HTML - by El Forum - 02-09-2009, 03:50 PM
Identifying images referenced in HTML - by El Forum - 02-09-2009, 04:53 PM
Identifying images referenced in HTML - by El Forum - 02-09-2009, 07:09 PM
Identifying images referenced in HTML - by El Forum - 02-09-2009, 07:12 PM
Identifying images referenced in HTML - by El Forum - 02-09-2009, 08:01 PM
Identifying images referenced in HTML - by El Forum - 02-09-2009, 08:03 PM
Identifying images referenced in HTML - by El Forum - 02-09-2009, 08:07 PM
Identifying images referenced in HTML - by El Forum - 02-09-2009, 09:34 PM



Theme © iAndrew 2016 - Forum software by © MyBB