Các tham số URL phân tích cú pháp PHP

Hàm parse_url() nhận một URL string và trả về một mảng kết hợp chứa các thành phần của URL

Cú pháp của hàm như sau

parse_url(
    string $url,
    int $component = -1
): int|string|array|null|false

Hàm parse_url() chấp nhận hai tham số

  • Chuỗi $url cần thiết để phân tích cú pháp
  • Số nguyên $component báo cho hàm truy xuất một thành phần URL cụ thể (tùy chọn)

Đây là một ví dụ về việc gọi hàm parse_url()

<?php

$url = parse_url("https://sebhastian.com/php-parse-url");
print_r($url);

// 👇 output
// Array
// (
//     [scheme] => https
//     [host] => sebhastian.com
//     [path] => /php-parse-url
// )

Đoạn mã trên cho thấy cách parse_url() trả về một mảng kết hợp gồm ba phần tử. các phần tử

<?php

$url = parse_url("https://sebhastian.com/php-parse-url");
print_r($url);

// 👇 output
// Array
// (
//     [scheme] => https
//     [host] => sebhastian.com
//     [path] => /php-parse-url
// )
1,
<?php

$url = parse_url("https://sebhastian.com/php-parse-url");
print_r($url);

// 👇 output
// Array
// (
//     [scheme] => https
//     [host] => sebhastian.com
//     [path] => /php-parse-url
// )
2 và
<?php

$url = parse_url("https://sebhastian.com/php-parse-url");
print_r($url);

// 👇 output
// Array
// (
//     [scheme] => https
//     [host] => sebhastian.com
//     [path] => /php-parse-url
// )
3

Tùy thuộc vào biểu mẫu URL của bạn, hàm có thể trả về các phần tử bên dưới

  • <?php
    
    $url = parse_url("https://sebhastian.com/php-parse-url");
    print_r($url);
    
    // 👇 output
    // Array
    // (
    //     [scheme] => https
    //     [host] => sebhastian.com
    //     [path] => /php-parse-url
    // )
    
    1
  • <?php
    
    $url = parse_url("https://sebhastian.com/php-parse-url");
    print_r($url);
    
    // 👇 output
    // Array
    // (
    //     [scheme] => https
    //     [host] => sebhastian.com
    //     [path] => /php-parse-url
    // )
    
    2
  • <?php
    
    $url = parse_url("https://sebhastian.com/php-parse-url");
    print_r($url);
    
    // 👇 output
    // Array
    // (
    //     [scheme] => https
    //     [host] => sebhastian.com
    //     [path] => /php-parse-url
    // )
    
    6
  • <?php
    
    $url = parse_url("https://sebhastian.com/php-parse-url");
    print_r($url);
    
    // 👇 output
    // Array
    // (
    //     [scheme] => https
    //     [host] => sebhastian.com
    //     [path] => /php-parse-url
    // )
    
    7
  • <?php
    
    $url = parse_url("https://sebhastian.com/php-parse-url");
    print_r($url);
    
    // 👇 output
    // Array
    // (
    //     [scheme] => https
    //     [host] => sebhastian.com
    //     [path] => /php-parse-url
    // )
    
    8
  • <?php
    
    $url = parse_url("https://sebhastian.com/php-parse-url");
    print_r($url);
    
    // 👇 output
    // Array
    // (
    //     [scheme] => https
    //     [host] => sebhastian.com
    //     [path] => /php-parse-url
    // )
    
    3
  • <?php
    echo parse_url(
        "https://sebhastian.com/php-parse-url",
        PHP_URL_HOST
    );
    
    // 👇 output:
    // sebhastian.com -> string
    
    0
  • <?php
    echo parse_url(
        "https://sebhastian.com/php-parse-url",
        PHP_URL_HOST
    );
    
    // 👇 output:
    // sebhastian.com -> string
    
    1

Bạn có thể chọn một thành phần cụ thể như sau

  • <?php
    echo parse_url(
        "https://sebhastian.com/php-parse-url",
        PHP_URL_HOST
    );
    
    // 👇 output:
    // sebhastian.com -> string
    
    2
  • <?php
    echo parse_url(
        "https://sebhastian.com/php-parse-url",
        PHP_URL_HOST
    );
    
    // 👇 output:
    // sebhastian.com -> string
    
    3
  • <?php
    echo parse_url(
        "https://sebhastian.com/php-parse-url",
        PHP_URL_HOST
    );
    
    // 👇 output:
    // sebhastian.com -> string
    
    4
  • <?php
    echo parse_url(
        "https://sebhastian.com/php-parse-url",
        PHP_URL_HOST
    );
    
    // 👇 output:
    // sebhastian.com -> string
    
    5
  • <?php
    echo parse_url(
        "https://sebhastian.com/php-parse-url",
        PHP_URL_HOST
    );
    
    // 👇 output:
    // sebhastian.com -> string
    
    6
  • <?php
    echo parse_url(
        "https://sebhastian.com/php-parse-url",
        PHP_URL_HOST
    );
    
    // 👇 output:
    // sebhastian.com -> string
    
    7
  • <?php
    echo parse_url(
        "https://sebhastian.com/php-parse-url",
        PHP_URL_HOST
    );
    
    // 👇 output:
    // sebhastian.com -> string
    
    8
  • <?php
    echo parse_url(
        "https://sebhastian.com/php-parse-url",
        PHP_URL_HOST
    );
    
    // 👇 output:
    // sebhastian.com -> string
    
    9

Đây là một ví dụ về việc chỉ truy xuất máy chủ lưu trữ của URL

<?php
echo parse_url(
    "https://sebhastian.com/php-parse-url",
    PHP_URL_HOST
);

// 👇 output:
// sebhastian.com -> string

Thành phần bạn muốn truy xuất sẽ được trả về dưới dạng string ngoại trừ

<?php
echo parse_url(
    "https://sebhastian.com/php-parse-url",
    PHP_URL_HOST
);

// 👇 output:
// sebhastian.com -> string
4 sẽ trả về một parse_url()2

Làm cách nào để lấy giá trị URL trong PHP?

Các biến siêu toàn cầu cần thiết như $_SERVER['HTTPS'], $_SERVER['REQUEST_URI'], $_SERVER['SERVER_PORT'] are used to get full URL in PHP. The variable HTTPS can easily retrieve the protocol in the URL of a webpage. If it returns a value “on”, then the protocol is HTTPS.

Làm cách nào để lấy giá trị mảng từ URL trong PHP?

Để làm điều đó, bạn có thể sử dụng siêu toàn cầu $_GET , đây là một mảng có mọi tham số URL trong đó. ? .

Làm cách nào để tách một URL trong PHP?

Làm cách nào để thêm tham số truy vấn vào URL trong PHP?

Cách đáng tin cậy để thêm một tham số GET cụ thể là gì? . echo http_build_url($url, array("query" => "the=query&parts=here"), HTTP_URL_JOIN_QUERY);. .
cũng đừng quên nối thêm giá trị của danh mục. .
@DougT. .
Cách tiếp cận này tốt khi bạn biết rằng tham số 'danh mục' chưa có trong URL