How to get image source from file using PHP

Wednesday, August 31st, 2011

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
Avatar of Ranjith

Author Name :
Ranjith

Total : 1 Comment


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

  1. bramayadi says:

    wow, thank’s for your info. this help me so much

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup

Email: