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:studentprojects.in/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:studentprojects.in/favicon.ico
Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in