반응형
📌 1. 함수 원형 (Function Prototype)
array|string|false parse_url(string $url, int $component = -1)
📌 2. 함수 설명 (Function Description)
parse_url() 함수는 주어진 URL 문자열을 구성 요소로 분해하여 배열로 반환합니다. URL의 구성 요소는 scheme, host, port, user, pass, path, query, fragment 등입니다.
선택적으로 $component 매개변수를 지정하면, 해당 구성 요소만 반환합니다.
만약 URL이 잘못되었을 경우 false를 반환합니다.
📌 3. 실행 가능한 PHP 버전
- 지원 시작: PHP 4.0.0
- PHP 5.1.2부터 IPv6 주소 지원
- PHP 8.0.0부터 반환형에 string|array|false 적용 (명확화됨)
📌 4. 함수 사용 예제 (Usage Example)
✅ 예제 1: URL 전체를 파싱하여 배열로 반환
<?php
$url = "https://user:pass@example.com:8080/path/to/page.php?foo=bar#section";
$parsed = parse_url($url);
print_r($parsed);
?>
출력 결과:
Array
(
[scheme] => https
[host] => example.com
[port] => 8080
[user] => user
[pass] => pass
[path] => /path/to/page.php
[query] => foo=bar
[fragment] => section
)
✅ 예제 2: 특정 구성 요소만 추출
<?php
$url = "https://www.example.com/path/index.html?search=test#top";
// path만 추출
$path = parse_url($url, PHP_URL_PATH);
echo $path; // 출력: /path/index.html
?>
반응형
'개발자 이야기 > PHP' 카테고리의 다른 글
parse_str() (0) | 2025.05.16 |
---|---|
http_build_query() (0) | 2024.06.21 |
URL Encode (0) | 2024.06.21 |
댓글