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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:56:06+00:00 2026-05-12T23:56:06+00:00

I have the following code below on my website. It’s used to find the

  • 0

I have the following code below on my website. It’s used to find the images in a block of html that don’t have http:// or / in front. If this is the case, it will add the website url to the front of the image source.

For example:

<img src="http://domain.com/image.jpg"> will stay the same
<img src="/image.jpg"> will stay the same
<img src="image.jpg"> will be changed to <img src="http://domain.com/image.jpg">

I feel my code is really inefficient… Any ideas on how I could make it run with less code?

preg_match_all('/<img[\s]+[^>]*src\s*=\s*[\"\']?([^\'\" >]+)[\'\" >]/i', $content_text, $matches);
if (isset($matches[1])) {
  foreach($matches[1] AS $link) {
    if (!preg_match("/^(https?|ftp)\:\/\//sie", $link) && !preg_match("/^\//sie", $link)) {
      $full_link = get_option('siteurl') . '/' . $link;
      $content_text = str_replace($link, $full_link, $content_text);
    }
  }
}
  • 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-12T23:56:07+00:00Added an answer on May 12, 2026 at 11:56 pm

    For a start you could stop using regular expressions to process HTML, particularly when what you’re doing is so easily done with an HTML parser (of which PHP has at least 3). For example:

    $dom = new DomDocoument;
    $dom->loadHTML($html);
    $images = $dom->getElementsByTagName('img');
    foreach ($images as $image) {
      $src = $image->getAttribute('src');
      $url = parse_url($src);
      $image->setAttribute('src', http_build_url('http://www.example.com', $url);
    }
    $html = $dom->saveHTML();
    

    Problem solved. Well, almost. The case where you add the hostname to relative URLs but not to those beginning with / is a little puzzling and not handled in this snippet but it’s a relatively minor change (it involves checking $url['path']).

    See Parse HTML With PHP And DOM, the Document Object Model, parse_url() and http_build_url(). PHP has much better tools for this than regular expressions.

    Oh and for good measure read Parsing Html The Cthulhu Way.

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

Sidebar

Related Questions

I have the following code in the header of a website that I'm working
i have the following code below: const char* timeformat = %Y-%m-%d %H:%M:%S; const int
I want to know is below code correct ? I have following code which
I have the following C# code below. Object first = 5; Object second =
i have the following code: <h3> Your Profile </h3> The information below is what
I have following Code Block Which I tried to optimize in the Optimized section
i have the following code below but sometime the serving_description tag isn't there. Right
I have a rails 3.0.3 website that, in development mode, responds to a HTTP
Let's say we have the following JavaScript/jQuery code below function createElement(i, value) { return
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public

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.