0

I'm trying to get image address from a img tag in PHP. this is a HTML page:

<div class="image">
    <a href="http://mywebpage.com/">
    <img height="317" width="214" alt="Photo" title="Photo" src="http://mydomain.com/image.jpg" itemprop="image">
    </a>
</div>

The PHP Part:

$text = file_get_contents("http://www.mydomain.com/page.html");

//i Tried This One:
preg_match_all('/<div class=\"image\">(.*?)<\/div>/s', $text, $out);

//And This one
preg_match('/~src="(.*)"itemprop="image" \/>/',$text,$out);

//Print
print_r($out);

Problem is, i can't fetch the image address only! i searched in Google and Stack Overflow and tried some codes.

I hope you guys help me with that problem.

Alireza
  • 1,048
  • 5
  • 18
  • 36
  • 3
    [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – nhahtdh Sep 18 '13 at 05:15
  • 1
    Your second pattern doesn't fit your text. Try `preg_match('~src="(.*)"\s*itemprop="image"[^>]*>~,$text,$out);` – Jerry Sep 18 '13 at 05:29

4 Answers4

1
First download simple_html_dom
from URL:
http://sourceforge.net/projects/simplehtmldom/

Then you find a file "simple_html_dom.php"

Create a file "getImageSrc.php" and include file "simple_html_dom.php" 

Write code bellow in getImageSrc.php :

<?php 
$url = "www.yoururl.com"; //
$html = file_get_html($url);

         foreach($html->find('img') as $e) {
            echo $e->src; //img src will be print. you can match your src which you want.
            echo "<br />";
    }
Vishnu Sharma
  • 1,357
  • 12
  • 11
1

Your second pattern is the one causing issues:

preg_match('/~src="(.*)"itemprop="image" \/>/',$text,$out);
             ^         ^^               ^^^
             1         2                 3
  1. Seems like a stray tilde. Either you use tilde, or you use forward slash as delimiter. Since we're having quite some forward slashes in the text to match, I suggest using the tilde.

  2. There's a space in the text but not in the regex. Maybe use a \s* just in case.

  3. There's nothing like that in the text. Though just in case there might be characters there, you could use [^>]* which means any character which is not a > 0 or more times.

Applying the three, we get:

preg_match('~src="(.*)"\s*itemprop="image"[^>]*>~',$text,$out);
Jerry
  • 70,495
  • 13
  • 100
  • 144
0
preg_match('/<img.*? src=\"(.*?)\".*?>/',$text,$out);

It works for me. Try this solution

geekdev
  • 1,192
  • 11
  • 15
0

Try this

preg_match('/src="(.*?)" itemprop="image"/',$text,$match);
print_r("match=>".$match[1]);
Jasmin Mistry
  • 1,459
  • 1
  • 18
  • 23