Php get base url from string

I have a simple script like this

$data = "//example.com/index.php?data=data&value=value";

How to get base url from $data? The output will be like this

//example.com

The output without this text " /index.php?data=data&value=value

asked Jun 2, 2018 at 4:09

3

Use parse_url[]

$url = "//example.com/index.php?data=data&value=value";
$url_info = parse_url[$url];
echo $url_info['host'];//hostname

Additionally $user_info contain below

[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor

SO you can get http by scheme

you can do something like :

echo $url_info['scheme'] . '://' . $url_info['host'];////example.com

answered Jun 2, 2018 at 4:15

Devsi OdedraDevsi Odedra

5,1231 gold badge22 silver badges33 bronze badges

0

Check Parsing Domain From URL In PHP

$url = '//google.com/dhasjkdas/sadsdds/sdda/sdads.html';
$parse = parse_url[$url];
echo $parse['host']; // prints 'google.com'

answered Jun 2, 2018 at 4:16

You can use pathinfo included scheme and hostname.

$url = "//example.com/index.php?data=data&value=value";
$base_url = pathinfo[$url, PATHINFO_DIRNAME];
echo $base_url;

Output:

//example.com

answered Apr 13, 2021 at 22:26

Nabi K.A.Z.Nabi K.A.Z.

8,7966 gold badges53 silver badges71 bronze badges

$url= parse_url[$url, PHP_URL_HOST];

Why can't we just use this. Worked for me.

answered Jun 30, 2021 at 14:21

Yasir IjazYasir Ijaz

5249 silver badges18 bronze badges

Base URL is used to create internal web page links dynamically in the website. You can get the base URL from the full URL string using PHP. The parse_url[] function helps to parse components from URL in PHP. The base URL can be retrieved from a string using PHP parse_url[] function.

The following code snippet shows how to get base URL from URL string with PHP.

  • PHP_URL_SCHEME – This component returns URL scheme [http/https].
  • PHP_URL_HOST – This componenet returns host name [example.com/www.example.com].
$url '//www.codexworld.com/how-to/get-current-url-php/'; 
$url parse_url[$urlPHP_URL_SCHEME].'://'.parse_url[$urlPHP_URL_HOST];
$base_url trim[$url'/'];

The above code [$base_url] will return the base URL from given string: //www.codexworld.com

[PHP 4, PHP 5, PHP 7, PHP 8]

parse_urlParse a URL and return its components

Description

parse_url[string $url, int $component = -1]: int|string|array|null|false

This function is not meant to validate the given URL, it only breaks it up into the parts listed below. Partial and invalid URLs are also accepted, parse_url[] tries its best to parse them correctly.

Parameters

url

The URL to parse.

component

Specify one of PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY or PHP_URL_FRAGMENT to retrieve just a specific URL component as a string [except when PHP_URL_PORT is given, in which case the return value will be an int].

Return Values

On seriously malformed URLs, parse_url[] may return false.

If the component parameter is omitted, an associative array is returned. At least one element will be present within the array. Potential keys within this array are:

  • scheme - e.g. http
  • host
  • port
  • user
  • pass
  • path
  • query - after the question mark ?
  • fragment - after the hashmark #

If the component parameter is specified, parse_url[] returns a string [or an int, in the case of PHP_URL_PORT] instead of an array. If the requested component doesn't exist within the given URL, null will be returned. As of PHP 8.0.0, parse_url[] distinguishes absent and empty queries and fragments:

//example.com/foo → query = null, fragment = null
//example.com/foo? → query = "",   fragment = null
//example.com/foo# → query = null, fragment = ""
//example.com/foo?# → query = "",   fragment = ""

Previously all cases resulted in query and fragment being null.

Note that control characters [cf. ctype_cntrl[]] in the components are replaced with underscores [_].

Changelog

VersionDescription
8.0.0 parse_url[] will now distinguish absent and empty queries and fragments.

Examples

Example #1 A parse_url[] example

The above example will output:

array[8] {
  ["scheme"]=>
  string[4] "http"
  ["host"]=>
  string[8] "hostname"
  ["port"]=>
  int[9090]
  ["user"]=>
  string[8] "username"
  ["pass"]=>
  string[8] "password"
  ["path"]=>
  string[5] "/path"
  ["query"]=>
  string[9] "arg=value"
  ["fragment"]=>
  string[6] "anchor"
}
string[4] "http"
string[8] "username"
string[8] "password"
string[8] "hostname"
int[9090]
string[5] "/path"
string[9] "arg=value"
string[6] "anchor"

Example #2 A parse_url[] example with missing scheme

The above example will output:

array[3] {
  ["host"]=>
  string[15] "www.example.com"
  ["path"]=>
  string[5] "/path"
  ["query"]=>
  string[17] "googleguy=googley"
}

Notes

Caution

This function may not give correct results for relative or invalid URLs, and the results may not even match common behavior of HTTP clients. If URLs from untrusted input need to be parsed, extra validation is required, e.g. by using filter_var[] with the FILTER_VALIDATE_URL filter.

Note:

This function is intended specifically for the purpose of parsing URLs and not URIs. However, to comply with PHP's backwards compatibility requirements it makes an exception for the file:// scheme where triple slashes [file:///...] are allowed. For any other scheme this is invalid.

See Also

  • pathinfo[] - Returns information about a file path
  • parse_str[] - Parses the string into variables
  • http_build_query[] - Generate URL-encoded query string
  • dirname[] - Returns a parent directory's path
  • basename[] - Returns trailing name component of path
  • » RFC 3986

thomas at gielfeldt dot com

10 years ago

[If you haven't yet] been able to find a simple conversion back to string from a parsed url, here's an example:

lauris [] lauris ! lv

8 years ago

Here is utf-8 compatible parse_url[] replacement function based on "laszlo dot janszky at gmail dot com" work. Original incorrectly handled URLs with user:pass. Also made PHP 5.5 compatible [got rid of now deprecated regex /e modifier].

mys5droid at gmail dot com

6 years ago

I have coded a function which converts relative URL to absolute URL for a project of mine. Considering I could not find it elsewhere, I figured I would post it here.

The following function takes in 2 parameters, the first parameter is the URL you want to convert from relative to absolute, and the second parameter is a sample of the absolute URL.

Currently it does not resolve '../' in the URL, only because I do not need it. Most webservers will resolve this for you. If you want it to resolve the '../' in the path, it just takes minor modifications.

Chủ Đề