简体   繁体   中英

PHP preg_match_all get fetch all images to array

Pretty much self explanatory...

Here is my code :

$html=<<<EOF

<img src="smiley.gif" alt="Smiley face" height="42" width="42"> 
<img  alt="title" height="120" width="50" src="title.jpg" />
<img  alt=Footer src=footer.bmp height=30 width=47     />
<br>
<img  alt=Footer  height=10 src='square.jpeg' width=10     />
<div id="test"><img      longdesc="" width=100 src="transparent.png" height=43></div>
EOF;

preg_match_all("//",$html,$images);

I want to be able to return all image files to an array with preg_match_all (or any other magic :)

I'm expecting to get an array with all pictures so var_dump($images); would be:

smiley.gif
title.jpg
footer.bmp
square.jpeg
transparent.png

you can use Simple HTML DOM Parser or PHP DOMDocument , vor example:

$doc = new DOMDocument();
$doc->loadHTML($htmlstring);
$elements = $doc->getElementsByTagName('img');

foreach($elements as $element) {
 print $element->getAttribute('src');
}
preg_match_all('/<img(.*?)src=("|\'|)(.*?)("|\'| )(.*?)>/s', $html, $images);
var_dump($images[3]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM