Home >Backend Development >PHP Tutorial >How to Extract the Domain from a URL Using PHP's `parse_url()` Function?

How to Extract the Domain from a URL Using PHP's `parse_url()` Function?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-10 06:08:13889browse

How to Extract the Domain from a URL Using PHP's `parse_url()` Function?

How to Parse Domain from a URL in PHP

In PHP, the parse_url() function can be used to extract the domain from a URL. This function returns an associative array containing various components of the URL, including the host key which corresponds to the domain.

Example:

To parse the domain from the following URL:

http://google.com/dhasjkdas/sadsdds/sdda/sdads.html

Use the following code:

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

Note:

  • parse_url() does not handle mangled URLs very well, but it works well for valid URLs.
  • parse_url() returns additional information about the URL, such as the scheme, path, query, and fragment.

The above is the detailed content of How to Extract the Domain from a URL Using PHP's `parse_url()` Function?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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