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
Release: 2024-12-10 06:08:13
Original
946 people have browsed it

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
Copy after login

Use the following code:

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

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!

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