1
$str = "html code here and img tag";
preg_match_all('~<img ([^>]+)>~i', $str, $matches);
$images = array();
foreach ($matches[1] as $str) {
    preg_match_all('~([a-z]([a-z0-9]*)?)=("|\')(.*?)("|\')~is', $str, $pairs);
}

I can't get SRC from IMG, how to ideas ?

Prisoner
  • 27,391
  • 11
  • 73
  • 102
Hai Truong IT
  • 4,126
  • 13
  • 55
  • 102
  • 1
    See: http://stackoverflow.com/questions/1058852/regex-to-get-src-value-from-an-img-tag – Andy Dec 14 '11 at 10:21

2 Answers2

3

I would recommend you consider using simplehtmldom instead of parsing out your img src tags with regular expressions.

$html = file_get_html('...html...');

// Find all images & echo src
foreach($html->find('img') as $element)
       echo $element->src . '<br>';
Michael Robinson
  • 29,278
  • 12
  • 104
  • 130
2

You can use an XML parser in PHP like Simple HTML Dom, or a regex for something like this should also work.

/src="([^"]*)"/i
Russell Dias
  • 70,980
  • 5
  • 54
  • 71