0

I have page image.php

where images are kept in container like below :- Note: There are other Images outside container div too.. i just want images from container div.

    <!DOCTYPE html>
    <head>
        <title>Image Holder</title>
    </head>
    <body>
        <header>
        <a href="#"><img src="http://examepl.com/logo.png"></a>
            <div id="side">
                <div id="facebook"><img src="http://examepl.com/fb.png"></div>
                <div id="twiiter"><img src="http://examepl.com/t.png"></div>
                <div id="gplus"><img src="http://examepl.com/gp.png"></div>
            </div>
        </header>      
        <div class="container">
            <p>SOme Post</p>
            <img src="http://examepl.com/some.png" title="some image" />

            <p>SOme Post</p>
            <img src="http://examepl.com/some.png" title="some image" />

            <p>SOme Post</p>
            <img src="http://examepl.com/some.png" title="some image" />
        </div>
        <footer>
            <div id="foot">
                copyright &copy; 2013
            </div>
        </footer>
    </body>
</html>

and i am trying to fetch only image from my image.php file with preg_match_all, but it returns boolean(false) :(

my php code :-

<?php
$file = file_get_contents("image.php");
preg_match_all("/<div class=\"container\">(.*?)</div>/", $file, $match);
preg_match_all("/<img src=\"(.*?)\">/", $match, $images);

var_dump($images);
?>

Both the files are in root folder , and now i am getting blank page :(

Any help would be great

Thanks

4 Answers 4

1

I think this will work for you try the link below to test your regex

preg_match_all("/<div class=\"container\">(.*?)<\/div>/", $file, $match);
preg_match_all("/<img .*?(?=src)src=\"([^\"]+)\"/", $match[1][0], $images);

http://www.phpliveregex.com

Sign up to request clarification or add additional context in comments.

1 Comment

it returns blank page :(
1

You better not use regex for this purpose. PHP provides nice DOM api for this purpose. Consider code like below:

$html = <<< EOF
<div class="container">
<p>SOme Post</p>
<img src="http://examepl.com/some1.png" title="some image" />
<p>SOme Post</p>
<img src="http://examepl.com/some2.png" title="some image" />
<p>SOme Post</p>
<img src="http://examepl.com/some3.png" title="some image" />
</div>
EOF;
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html); // loads your html
$xpath = new DOMXPath($doc);
$nodelist = $xpath->query("//div[@class='container']/img");
$img = array();
for($i=0; $i < $nodelist->length; $i++) {
    $node = $nodelist->item($i);
    $img[] = $node->getAttribute('src');
}
print_r($img);

OUTPUT:

Array
(
    [0] => http://examepl.com/some1.png
    [1] => http://examepl.com/some2.png
    [2] => http://examepl.com/some3.png
)

Live Demo: http://ideone.com/iBhVMF

5 Comments

i have 2 domain, and i might have to use examepl.com/image.php to fetch image in my another domain :(
You can use your $file variable to load DOM like this: $doc->loadHTML($file);
i tried your code for $file in examepl.com/image.php and it returned Array( ) :(
Can you post HTML content held in $file variable.
I took the image.php posted by you in your question and it worked fine.
0

You can easily obtain what you want with an XPath query:

$url = 'http://examepl.com/image.php';

$doc = new DOMDocument();
@$doc->loadHTMLFile($url);
$xpath = new DOMXPath($doc);
$srcs = $xpath->query("//div[@class='container']//img/attribute::src");
foreach ($srcs as $src) {
    echo '<br/>' . $src->value; 
}

4 Comments

i have another domain who will also need to fetch images from my first domain too... and i dont want to write different code for my both the sites..
@DragonSnake: It's not a problem, write a function with the filename as argument.
well i replaced @$doc->loadHTMLFile('image.php'); with this @$doc->loadHTMLFile($file); where my $file is examepl.com/image.php and it returned blank image :(
@DragonSnake: you don't need to use file_get_contents, see my edit.
0
preg_match_all("/<img src=\"(.*?)\">/", $match, $images);

replace with

preg_match_all("/<img src=\"(.*?)\"/", $match, $images); // stripped ">" char

2 Comments

not fetching image, it says array[2] 0 =>array[0] => {} array[1] => {} :(
Can you post the contents of image.php? If there's a loop displaying images - it won't work as the file_get_contents will get the php code, not the output for browsers ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.