1

Possible Duplicate:
How to get IMG tag code from HTML document?

I need help to make preg_match_all() for every image URL on random page.
As far I do this

preg_match_all('/img[\d\D]+?src\=(\'|\")([\d\D]+?)(\'|\")/i', $page, $matches); 

But won't work for every page. Must match all possible image closed in img src also ones that doesn't look like images. thank you

Community
  • 1
  • 1
T1000
  • 2,909
  • 7
  • 36
  • 53
  • Regex is not the right tool here. Have a look at a HTML parser. – Jens Oct 06 '10 at 09:07
  • Nope, what do you mean ? Can you give me link or something? But will be nice if you help me with the regex - cause It take me lot of time and I want to see it done :) – T1000 Oct 06 '10 at 09:17

1 Answers1

1

use the html DOM parser -> http://simplehtmldom.sourceforge.net/

all you need to do then is use this code:

// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');

// Find all images
foreach($html->find('img') as $element)
       echo $element->src . '<br>';
Christophe
  • 4,798
  • 5
  • 41
  • 83
  • Thanks, but I really don't need to include all that code at the moment - script is too simple for that, but maybe will be helpful later. – T1000 Oct 06 '10 at 09:28
  • 10x again - that parser is very cewl – T1000 Oct 06 '10 at 11:26
  • 1
    Suggested third party alternatives to [SimpleHtmlDom](http://simplehtmldom.sourceforge.net/) that actually use [DOM](http://php.net/manual/en/book.dom.php) instead of String Parsing: [phpQuery](http://code.google.com/p/phpquery/), [Zend_Dom](http://framework.zend.com/manual/en/zend.dom.html), [QueryPath](http://querypath.org/) and [FluentDom](http://www.fluentdom.org). – Gordon Oct 06 '10 at 12:46