Home > Backend Development > PHP Tutorial > How to Convert a Relative Path to an Absolute URL Using PHP?

How to Convert a Relative Path to an Absolute URL Using PHP?

Susan Sarandon
Release: 2024-11-07 11:52:03
Original
217 people have browsed it

How to Convert a Relative Path to an Absolute URL Using PHP?

Transforming Relative Paths into Absolute URLs with PHP

Converting relative paths into absolute URLs is essential for working with web-based resources. This task can be accomplished using PHP, a widely-used scripting language for web development.

How to Convert a Relative Path to an Absolute URL Using PHP:

To achieve this conversion, you can utilize the rel2abs() function:

function rel2abs($rel, $base)
{
    /* return if already absolute URL */
    if (parse_url($rel, PHP_URL_SCHEME) != '') return $rel;

    /* queries and anchors */
    if ($rel[0]=='#' || $rel[0]=='?') return $base.$rel;

    /* parse base URL and convert to local variables:
       $scheme, $host, $path */
    extract(parse_url($base));

    /* remove non-directory element from path */
    $path = preg_replace('#/[^/]*$#', '', $path);

    /* destroy path if relative url points to root */
    if ($rel[0] == '/') $path = '';

    /* dirty absolute URL */
    $abs = "$host$path/$rel";

    /* replace '//' or '/./' or '/foo/../' with '/' */
    $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
    for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}

    /* absolute URL is ready! */
    return $scheme.'://'.$abs;
}
Copy after login

Explanation:

  1. Check if the relative path is already an absolute URL (contains a scheme).
  2. Concatenate queries or anchors with the base URL.
  3. Parse the base URL into its scheme, host, and path components.
  4. Convert the provided relative path into a "dirty" absolute URL.
  5. Iteratively replace patterns like "//" or "/./" or "/foo/../" with "/" to clean up the absolute URL.
  6. Return the cleaned absolute URL.

Usage:

You can utilize this function to transform any relative path into its corresponding absolute URL. For example, the following snippets depict how to transform a relative path into an absolute URL:

$rel_path = "images/profile.jpg";
$base_url = "https://example.com/";

$abs_url = rel2abs($rel_path, $base_url); // Output: "https://example.com/images/profile.jpg"
Copy after login

The above is the detailed content of How to Convert a Relative Path to an Absolute URL Using PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template