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 7812803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:38:01+00:00 2026-06-02T04:38:01+00:00

I am having a problem trying to get this snippet of code to output

  • 0

I am having a problem trying to get this snippet of code to output a separate og:image meta tag for each image in the array.

 <?php    function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){
        //Defines a default image
        $first_img = "http://example.com/images/fbthumb.jpg";
    }
return $first_img;
}
 ?>

Currently the code is returning the first matched img src tag, but I would like it to return them all as separate tags so I can select which share image to use.

I know this line:

   $first_img = $matches [1] [0];

needs to be changed into some type of for each condition but Im not sure how. Any help would be greatly appreciated. Thanks!

EDIT:

Here is my code after the last suggestion:

<?php function catch_that_image() {
global $post, $posts;
$result = preg_match_all('#<img.+src=[\'"]([^\'"]+)[^>]*>#i', $post->post_content, $matches);

return $matches[1];

}

?>

” />

I still cant figure it out. Any ideas?

  • 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-06-02T04:38:03+00:00Added an answer on June 2, 2026 at 4:38 am

    Try this function. It will return an array of image sources. You could then always use the first image in the array as default image.

    function catch_that_image() {
        global $post, $posts;
        $imgSources = array();
        $result = preg_match_all('#<img.+src=[\'"]([^\'"]+)[^>]*>#i', $post->post_content, $matches);
    
        foreach($matches[1] as $match) {
            $imgSources[] = $match;
        }
    
        return $imgSources;
    }
    

    or simpler

    function catch_that_image() {
        global $post, $posts;
        $result = preg_match_all('#<img.+src=[\'"]([^\'"]+)[^>]*>#i', $post->post_content, $matches);
    
        return $matches[1];
    }
    

    Using the function:

    $imageArray = catch_that_image();
    foreach($imageArray as $image) {
        echo '<meta property=​"og:​image" content=​"$image">';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some problems trying to get the code below to output the data
Hello I am new at Haskell and i'm having problems trying to get this
I am trying to get started with PEAR's HTML_QuickForm but I'm having a problem.
I am having a bit of a problem, I am trying to get a
I'm having a problem trying to format the output on the jQuery UI datepicker.
I'm having a problem when trying to pass an array back to a COM
I'm having an annoying problem trying to get an html page with a google
I'm having the following problem when trying to get the path of a given
Hey, I currently am having trouble trying to get this to work. Here's a
I am having trouble trying to get this script to work. When I debug

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.