Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7180895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:26:39+00:00 2026-05-28T17:26:39+00:00

I want to export the first image link from given text with PHP. Let’s

  • 0

I want to export the first image link from given text with PHP. Let’s say I have text like this:

Lorem ipsum <img rel="lorem" src="lorem.jpg"/> dolor sit amet, consectetuer <IMG src="ipsu.jpg" rel="ipsum"/ >

I need to export lorem.jpg to the variable in PHP. So, for example, finally $variable must be equal to lorem.jpg.
I used regular expressions, stripos and so on functions, but everytime there was some problem.
If you have any idea to solve this, please help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:26:40+00:00Added an answer on May 28, 2026 at 5:26 pm

    Regular expressions can be good for a large variety of tasks, but it usually fails when parsing HTML DOM. The problem with HTML is that the structure of your document is so variable that it is hard to accurately extract a tag.

    We can use DOM parser such as SimpleHTML

    You can use it like :

    $html = 'Lorem ipsum <img rel="lorem" src="lorem.jpg"/> dolor sit amet, consectetuer <img src="ipsu.jpg" rel="ipsum"/ > ';
    
    $first_image_source = get_first_image($html);
    echo $first_image_source;
    
    function get_first_image($html){
    
      require_once('simple_html_dom.php');
    
      $post_dom = str_get_html($html);
    
      $first_img = $post_dom->find('img', 0);
    
      if($first_img !== null) {
          return $first_img->src;
      }
    
      return null;
    }
    

    You can also get the alt attribute of the image in the same way.

    If you want to get source of all images then you can use:

    function get_images($html){
    
         require_once('simple_html_dom.php')
    
         $post_dom = str_get_html($html);
    
         $img_tags = $post_dom->find('img');
    
         $images = array();
    
         foreach($img_tags as $image) {
            $images[] = $image->src;
         }
    
         return $images;
      }
    

    Hope this helps 🙂 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a database that contains countries and cities. i want to export this
Now I want to export a image in my Project to a folder ,
I have a simple report that I want to export to a CSV file.
I have a huge IQueryable that I want to export directly to Excel for
I want to open an existing Movie-File and export every frame of this file
So I want to export the data from one list into another so I
I have two ServiceContracts implemented as interfaces. I want to export the metadata for
I am using a PHP script to export data from MySQL into Excel. The
I have a form which I want the user to be able to export
I need to regularly export XML files from our Administration software. This is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.