A common use for parse_url is processing data without rebuilding the operation manually. Parses URL components such as scheme, host, port, path and query without validating whether a URL is safe.
What Is parse_url in PHP?
Parses URL components such as scheme, host, port, path and query without validating whether a URL is safe.
Syntax:
parse_url(?string $url, ?int $component = ...) : array|string|int|false|null
Parameters and Return Value
$url— A required string input for this operation. Verify the allowed values in the examples and context below.$component— An optional int input for this operation. Verify the allowed values in the examples and context below.
Declared return type: array|string|int|false|null. Distinguish false, an empty result, and exceptions where the API permits them.
Example 1: Basic Usage
This example demonstrates the main operation with a small, controlled input.
<?php
print_r(parse_url("https://example.test/docs?page=2"));
The example focuses on the value produced or the state changed by this API. Review the return value and validate input before using this pattern in production.
Example 2: Another Input
This example changes the input or surrounding setup to illustrate another behavior.
<?php
echo parse_url("https://example.test/a", PHP_URL_HOST);
The example focuses on the value produced or the state changed by this API. Review the return value and validate input before using this pattern in production.
Practical Example
This application-style example shows a self-contained usage pattern and checks its result.
<?php
$parts = parse_url("https://example.test/orders?status=open");
if ($parts === false || ($parts["scheme"] ?? "") !== "https") { throw new InvalidArgumentException("Invalid URL"); }
echo $parts["path"] ?? "/";
The example focuses on the value produced or the state changed by this API. Review the return value and validate input before using this pattern in production.
Important Behavior and Edge Cases
- Parsing is not SSRF or redirect validation; enforce an allowed scheme and destination.
- Use the intended input type and handle empty or malformed values explicitly.
- Where this API requires an extension, version, or file, document that dependency before using it in a real application.
Comparison With Related Functions
| Function or Method | When to Use It |
|---|---|
parse_url | Parses URL components such as scheme, host, port, path and query without validating whether a URL is safe. |
rawurlencode | Percent-encodes a string as a URL component using RFC 3986 rules, representing spaces as %20. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using this API without checking the required input or extension | Parsing is not SSRF or redirect validation; enforce an allowed scheme and destination. |
| Encoding an entire URL as a component | Encode only the relevant path segment or query value. |
| Using decoded values as paths without validation | Enforce safe path boundaries after decoding. |
Key Takeaways
- Parses URL components such as scheme, host, port, path and query without validating whether a URL is safe.
- Parsing is not SSRF or redirect validation; enforce an allowed scheme and destination.
- Test both the expected case and at least one failure or empty-input case before deployment.
Parses URL components such as scheme, host, port, path and query without validating whether a URL is safe. Use it when the operation fits your input, output and validation requirements.