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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:09:17+00:00 2026-05-26T07:09:17+00:00

I have some text with images within it. I want to replace specific images

  • 0

I have some text with images within it. I want to replace specific images within the text with something else.

i.e. the text contains an a youtube img url that I want to replace with the actual video link.

<img class="mceItem" src="http://img.youtube.com/vi/1MsVzAkmds0/default.jpg" alt="1MsVzAkmds0">

and replace it with the youtube Iframe code:

<iframe title="'.$id.'" class="youtube-player" type="text/html" width="576" height="400" src="http://www.youtube.com/embed/'.$id.'" frameborder="0"></iframe>

my function looks like this:

function replacelink($link) {
  $find= ("/<img src=[^>]+\>/i");
  $replace = youtube("\\2");
  return preg_replace($find,$replace);
}

What do I need to change in the regex to do the above?

  • 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-26T07:09:18+00:00Added an answer on May 26, 2026 at 7:09 am

    Your regex is looking for <img src=, but there is a class attribute between img and src. Using $find= '/<img.*src=[^>]+>/i'; corrects the problem; however, this illustrates why you shouldn’t use regex to parse HTML.

    You wrote:

    I have some text with images within it.

    If the text you’re referring to is actually HTML, then there are better alternatives to using regex for this.

    Update

    I believe this is what you’re looking for.

    <?php
    function replacelink($text) {
        $replace = '<iframe title="$2" class="youtube-player" type="text/html" width="576" height="400" <iframe title="$2" class="youtube-player" type="text/html" width="576" height="400" src="http://www.youtube.com/embed/$2" frameborder="0"></iframe>';
        $find = '/(<img.*?alt="([\da-z]+)".*?>)/i';
        return preg_replace($find, $replace, $text);
    }
    
    $imagestr = '<img class="mceItem" src="http://img.youtube.com/vi/1MsVzAkmds0/default.jpg" alt="1MsVzAkmds0">';
    echo replacelink($imagestr);
    ?>
    

    There’s no need for a separate youtube() function.

    If you want to replace more than one image, use preg_replace_all() instead of preg_replace().

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

Sidebar

Related Questions

I have some text/images that I only want to display in the normal state
I have some text that has HTML hyper-links in it. I want to remove
I have some images that I want to display with a watermark. Currently they
I have some text that is stored in a variable like this: <div class=foo>text
I have some text that uses Unicode punctuation, like left double quote, right single
I have some text in a UITextView, that I would like to have show
I have some images in a SQL Server database. I want to display them
I have a script which collects an image, link and some text content from
I have a custom UITableViewCell which has an image & and some text. When
I have some text displaying in a larger font size than what it is

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.