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

  • Home
  • SEARCH
  • 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 8858989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:57:03+00:00 2026-06-14T14:57:03+00:00

There are a lot of posts on converting relative to absolute paths in PHP.

  • 0

There are a lot of posts on converting relative to absolute paths in PHP. I’m looking for a specific implementation beyond these posts (hopefully). Could anyone please help me with this specific implementation?

I have a PHP variable containing diverse HTML, including hrefs and imgs containing relative urls. Mostly (for example) /en/discover or /img/icons/facebook.png

I want to process this PHP variable in such a way that the values of my hrefs and imgs will be converted to http://mydomain.com/en/discover and http://mydomain.com/img/icons/facebook.png

I believe the question below covers the solution for hrefs. How can we expand this to also consider imgs?

  • Change a relative URL to absolute URL

Would a regex be in order? Or since we’re dealing with a lot of output should we use DOMDocument?

  • 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-14T14:57:04+00:00Added an answer on June 14, 2026 at 2:57 pm

    After some further research I’ve stumbled upon this article from Gerd Riesselmann on how to solve the absence of a base href solution for RSS-feeds. His snippet actually solves my question!

    http://www.gerd-riesselmann.net/archives/2005/11/rss-doesnt-know-a-base-url

    <?php
    function relToAbs($text, $base)
    {
      if (empty($base))
        return $text;
      // base url needs trailing /
      if (substr($base, -1, 1) != "/")
        $base .= "/";
      // Replace links
      $pattern = "/<a([^>]*) " .
                 "href=\"[^http|ftp|https|mailto]([^\"]*)\"/";
      $replace = "<a\${1} href=\"" . $base . "\${2}\"";
      $text = preg_replace($pattern, $replace, $text);
      // Replace images
      $pattern = "/<img([^>]*) " . 
                 "src=\"[^http|ftp|https]([^\"]*)\"/";
      $replace = "<img\${1} src=\"" . $base . "\${2}\"";
      $text = preg_replace($pattern, $replace, $text);
      // Done
      return $text;
    }
    ?>
    

    Thank you Gerd! And thank you shadyyx to point me in the direction of base href!

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

Sidebar

Related Questions

There are a lot of posts to show to create a virtual floppy image
There are a lot of posts on here about this, I'm using this code
UPDATE - There are a lot of posts regarding the Child actions are not
Although there are a lot of posts about .net config files, I believe that
I know there are a lot of posts related to this particular error but
I know there are a lot of posts here pertaining IE7 and float issues
I know there's a lot of posts with the same that problem. I just
There is a lot of posts about nested Ajax problems, but I can't figure
There are a lot of posts about how cool POCO objects are and how
I know there already are a lot of posts floating on the web regarding

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.