Heim Backend-Entwicklung PHP-Tutorial Wie extrahiere ich die Basisdomäne aus URLs mit unterschiedlichen Formaten in PHP?

Wie extrahiere ich die Basisdomäne aus URLs mit unterschiedlichen Formaten in PHP?

Oct 17, 2024 pm 05:12 PM

How to Extract the Base Domain from URLs with Varying Formats in PHP?

How to Extract the Primary Domain Name from a Variety of URL Formats in PHP

Introduction:

When working with URLs, it is often necessary to extract the primary domain name, excluding any subdomains. This can be a challenging task due to the wide range of URL formats that exist.

Solution:

Fortunately, there is a relatively straightforward solution using a combination of PHP functions. By utilizing parse_url and some string manipulation techniques, we can effectively extract the primary domain name from any URL format.

Step 1: Parse the URL

Using parse_url, we can extract the host part of the URL, which includes both the primary domain and any subdomains.

<code class="php">$info = parse_url($url);
$host = $info['host'];</code>
Nach dem Login kopieren

Step 2: Separate the Host Name and TLD

We then split the host string into an array based on periods using the explode function. The second to last string in the array represents the hostname, while the last string represents the top-level domain (TLD).

<code class="php">$host_names = explode(".", $host);
$bottom_host_name = $host_names[count($host_names)-2] . "." . $host_names[count($host_names)-1];</code>
Nach dem Login kopieren

Step 3: Combine the Hostname and TLD

By concatenating the hostname and TLD strings, we obtain the primary domain name.

<code class="php">echo $bottom_host_name;</code>
Nach dem Login kopieren

Explanation:

  • parse_url effectively extracts the host portion of the URL, including subdomains.
  • explode breaks the host into an array based on periods, allowing isolation of hostname and TLD.
  • count determines the number of elements in the array to identify the second-to-last string (hostname) and last string (TLD).
  • Concatenation combines the hostname and TLD into the primary domain name.

Das obige ist der detaillierte Inhalt vonWie extrahiere ich die Basisdomäne aus URLs mit unterschiedlichen Formaten in PHP?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
2 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Repo: Wie man Teamkollegen wiederbelebt
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Abenteuer: Wie man riesige Samen bekommt
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

11 beste PHP -URL -Shortener -Skripte (kostenlos und Premium) 11 beste PHP -URL -Shortener -Skripte (kostenlos und Premium) Mar 03, 2025 am 10:49 AM

11 beste PHP -URL -Shortener -Skripte (kostenlos und Premium)

Einführung in die Instagram -API Einführung in die Instagram -API Mar 02, 2025 am 09:32 AM

Einführung in die Instagram -API

Arbeiten mit Flash -Sitzungsdaten in Laravel Arbeiten mit Flash -Sitzungsdaten in Laravel Mar 12, 2025 pm 05:08 PM

Arbeiten mit Flash -Sitzungsdaten in Laravel

Erstellen Sie eine React -App mit einem Laravel -Back -Ende: Teil 2, reagieren Erstellen Sie eine React -App mit einem Laravel -Back -Ende: Teil 2, reagieren Mar 04, 2025 am 09:33 AM

Erstellen Sie eine React -App mit einem Laravel -Back -Ende: Teil 2, reagieren

Vereinfachte HTTP -Reaktion verspottet in Laravel -Tests Vereinfachte HTTP -Reaktion verspottet in Laravel -Tests Mar 12, 2025 pm 05:09 PM

Vereinfachte HTTP -Reaktion verspottet in Laravel -Tests

Curl in PHP: So verwenden Sie die PHP -Curl -Erweiterung in REST -APIs Curl in PHP: So verwenden Sie die PHP -Curl -Erweiterung in REST -APIs Mar 14, 2025 am 11:42 AM

Curl in PHP: So verwenden Sie die PHP -Curl -Erweiterung in REST -APIs

12 Beste PHP -Chat -Skripte auf Codecanyon 12 Beste PHP -Chat -Skripte auf Codecanyon Mar 13, 2025 pm 12:08 PM

12 Beste PHP -Chat -Skripte auf Codecanyon

Ankündigung von 2025 PHP Situation Survey Ankündigung von 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

Ankündigung von 2025 PHP Situation Survey

See all articles