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("//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("//sebhastian.com/php-parse-url"); print_r($url); // 👇 output // Array // ( // [scheme] => https // [host] => sebhastian.com // [path] => /php-parse-url // ) 1, <?php $url = parse_url("//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("//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("//sebhastian.com/php-parse-url"); print_r($url); // 👇 output // Array // ( // [scheme] => https // [host] => sebhastian.com // [path] => /php-parse-url // ) 1
- <?php $url = parse_url("//sebhastian.com/php-parse-url"); print_r($url); // 👇 output // Array // ( // [scheme] => https // [host] => sebhastian.com // [path] => /php-parse-url // ) 2
- <?php $url = parse_url("//sebhastian.com/php-parse-url"); print_r($url); // 👇 output // Array // ( // [scheme] => https // [host] => sebhastian.com // [path] => /php-parse-url // ) 6
- <?php $url = parse_url("//sebhastian.com/php-parse-url"); print_r($url); // 👇 output // Array // ( // [scheme] => https // [host] => sebhastian.com // [path] => /php-parse-url // ) 7
- <?php $url = parse_url("//sebhastian.com/php-parse-url"); print_r($url); // 👇 output // Array // ( // [scheme] => https // [host] => sebhastian.com // [path] => /php-parse-url // ) 8
- <?php $url = parse_url("//sebhastian.com/php-parse-url"); print_r($url); // 👇 output // Array // ( // [scheme] => https // [host] => sebhastian.com // [path] => /php-parse-url // ) 3
- <?php echo parse_url( "//sebhastian.com/php-parse-url", PHP_URL_HOST ); // 👇 output: // sebhastian.com -> string 0
- <?php echo parse_url( "//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( "//sebhastian.com/php-parse-url", PHP_URL_HOST ); // 👇 output: // sebhastian.com -> string 2
- <?php echo parse_url( "//sebhastian.com/php-parse-url", PHP_URL_HOST ); // 👇 output: // sebhastian.com -> string 3
- <?php echo parse_url( "//sebhastian.com/php-parse-url", PHP_URL_HOST ); // 👇 output: // sebhastian.com -> string 4
- <?php echo parse_url( "//sebhastian.com/php-parse-url", PHP_URL_HOST ); // 👇 output: // sebhastian.com -> string 5
- <?php echo parse_url( "//sebhastian.com/php-parse-url", PHP_URL_HOST ); // 👇 output: // sebhastian.com -> string 6
- <?php echo parse_url( "//sebhastian.com/php-parse-url", PHP_URL_HOST ); // 👇 output: // sebhastian.com -> string 7
- <?php echo parse_url( "//sebhastian.com/php-parse-url", PHP_URL_HOST ); // 👇 output: // sebhastian.com -> string 8
- <?php echo parse_url( "//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( "//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( "//sebhastian.com/php-parse-url", PHP_URL_HOST ); // 👇 output: // sebhastian.com -> string 4 sẽ trả về một parse_url()2