0

Let's say we have two variables:

$base = "http://some.base.url/script.php?query=string";
$link = "./anotherScript.php?query=anotherString";

Is there any way in PHP to combine those URL parts into:

$result = "http://some.base.url/anotherScript.php?query=anotherString";

?

SYNCRo
  • 450
  • 5
  • 21

4 Answers4

2

You can use parse_url function for parse base url also str_replace for $link.

in example;

$base = "http://some.base.url/script.php?query=string";
$link = "./anotherScript.php?query=anotherString";
$scheme = parse_url($base);
$link = str_replace('./','/',$link);
echo $scheme["scheme"]."://".$scheme["host"].$link;

It will give echo http://some.base.url/anotherScript.php?query=anotherString

merdincz
  • 427
  • 4
  • 16
0

I use this function in my own project.

public static function uri($base, $com)
  {
    // check base
    if(preg_match('/(https?|ftp|tcp|udp):\/\/([\w]+\.)?([\w]{4,})((\.\w{2,5}){1,2})/', $base)){
      $base = parse_url($base);

      if(preg_match('/^\//', $com)){
        $base['path'] = $com;
        unset($base['query']);
      }elseif (preg_match('/^\.\//', $com)) {
        $base['path'] = strrev(preg_replace('/^[\w\s-_\.]+\//',"",strrev($base['path'])));
        $base['path'] .= substr($com, 1);
        unset($base['query']);
      }else if(parse_url($com , PHP_URL_QUERY) != null){
        if(isset($base['query'])){
          $base['query'] .= '&'.substr($com, 1);
        }else{
          $base['query'] = substr($com, 1);
        }
      }

      if(!isset($base['query'])) return $base["scheme"]."://".$base["host"].$base['path'];
      else return $base["scheme"]."://".$base["host"].$base['path'].'?'.$base['query'];

    }else {
      return false;
    }
  }

Example outputs:

var_dump(Combine::uri("http://example.com/deneme.php?a=asdasd","/index.php"));
// string(35) "http://globalmedia.com.tr/index.php"

var_dump(Combine::uri("http://example.com/deneme.php?a=asdasd","/index.php?deneme=test"));
// string(47) "http://globalmedia.com.tr/index.php?deneme=test"

var_dump(Combine::uri("http://example.com/deneme.php?a=asdasd","?add=test"));
// string(54) "http://globalmedia.com.tr/deneme.php?a=asdasd&add=test"

var_dump(Combine::uri("http://example.com/deneme/asdasd","?a=asdasd"));
// string(48) "http://globalmedia.com.tr/deneme/asdasd?a=asdasd"

var_dump(Combine::uri("http://example.com/deneme/asdasd.php","./index.php"));
// string(42) "http://globalmedia.com.tr/deneme/index.php"

0

PHP.net has example functions in the comments section of the parse_url function that worked for me: https://www.php.net/manual/en/function.parse-url.php

The useful example will look something like this:

    /**
     * Resolve a URL relative to a base path. This happens to work with POSIX
     * filenames as well. This is based on RFC 2396 section 5.2.
     */
    function resolve_url($base, $url) {
        if (!strlen($base)) return $url;
        // Step 2
        if (!strlen($url)) return $base;
        // Step 3
        if (preg_match('!^[a-z]+:!i', $url)) return $url;
        $base = parse_url($base);
        if ($url{0} == "#") {
            // Step 2 (fragment)
            $base['fragment'] = substr($url, 1);
            return $this->unparse_url($base);
        }
        unset($base['fragment']);
        unset($base['query']);
        if (substr($url, 0, 2) == "//") {
            // Step 4
            return $this->unparse_url(array(
                'scheme'=>$base['scheme'],
                'path'=>$url,
            ));
        } else if ($url{0} == "/") {
            // Step 5
            $base['path'] = $url;
        } else {
            // Step 6
            $path = explode('/', $base['path']);
            $url_path = explode('/', $url);
            // Step 6a: drop file from base
            array_pop($path);
            // Step 6b, 6c, 6e: append url while removing "." and ".." from
            // the directory portion
            $end = array_pop($url_path);
            foreach ($url_path as $segment) {
                if ($segment == '.') {
                    // skip
                } else if ($segment == '..' && $path && $path[sizeof($path)-1] != '..') {
                    array_pop($path);
                } else {
                    $path[] = $segment;
                }
            }
            // Step 6d, 6f: remove "." and ".." from file portion
            if ($end == '.') {
                $path[] = '';
            } else if ($end == '..' && $path && $path[sizeof($path)-1] != '..') {
                $path[sizeof($path)-1] = '';
            } else {
                $path[] = $end;
            }
            // Step 6h
            $base['path'] = join('/', $path);

        }
        // Step 7
        return $this->unparse_url($base);
    }

    public function unparse_url($parsed_url) {

        $scheme   = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
        $host     = isset($parsed_url['host']) ? $parsed_url['host'] : '';
        $port     = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
        $user     = isset($parsed_url['user']) ? $parsed_url['user'] : '';
        $pass     = isset($parsed_url['pass']) ? ':' . $parsed_url['pass']  : '';
        $pass     = ($user || $pass) ? "$pass@" : '';
        $path     = isset($parsed_url['path']) ? $parsed_url['path'] : '';
        $query    = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
        $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';

        return "$scheme$user$pass$host$port$path$query$fragment";
    }
stephenfrank
  • 2,805
  • 2
  • 18
  • 16
-2
<?php
$url = explode('/', $base);
array_pop($url);
$result = implode('/', $url).substr($link, 1);; 
?>
Mathan Kumar
  • 55
  • 1
  • 6