How to get image source from file using PHP

Use preg_match_all to get the image source from your file.

PHP code:

1
2
3
4
5
6
7
8
<?php 
	$content='Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
        Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, 
        when an unknown printer took a galley of type and scrambled it to make a type specimen book.
        <a href="/"><img alt="" id="logo" src="http:electrofriends.com/favicon.ico"></a>';
        $frst_image = preg_match_all( '|<img.*?src=[\'"](.*?)[\'"].*?>|i', $content, $matches );
	echo $item['image'] = $matches[ 1 ][ 0 ];
?>

In the above code variable $content will have the complete source code. Searching the first image using preg_match_all

Output:

1
http:electrofriends.com/favicon.ico

One Response to “How to get image source from file using PHP”

Leave a Reply