Get all images from a HTML document Using PHP
Dec 13, 2013, by admin
Sometimes we have to get the images from an HTML page just like Facebook does when a link is posted. You can easily get images from HTML page using PHP. Below example code will return all images from a HTML document, you just need to get the source content of any HTML page and pass through below PHP function.
- <?php
- function getImagesFromHTMLDocument($data)
- {
- $images = array();
- preg_match_all(‘/<img [^>]*src=[“|’]([^”|’]+)/i’, $data, $matches);
- foreach ($matches[1] as $key=>$value) {
- $info = pathinfo($value);
- if (isset($info[‘extension’]))
- {
- if (($info[‘extension’] == ‘jpg’) ||
- ($info[‘extension’] == ‘jpeg’) ||
- ($info[‘extension’] == ‘gif’) ||
- ($info[‘extension’] == ‘png’))
- array_push($images, $value);
- }
- }
- return $images;
- }
- $img=getImagesFromHTMLDocument($data);
- print_r($img);
- ?>
To get more updates like the page Bugtreat Technologies and like the page Cs Cart Template for our Template updates