How can I get parameters from a URL string?

How can I get parameters from a URL string?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Parsing URL Parameters in PHP: Extracting Specific Query Values

When you receive a raw URL string from a $_POST field, the immediate challenge is distinguishing between the path components and the query parameters. In your specific scenario, where values like https://example.com/test/1234?email=xyz@test.com land in $_POST['url'], you need to isolate the email parameter without relying on $_GET or direct browser requests.

The first step is always normalization. URLs submitted via forms often contain trailing slashes, missing spaces around query delimiters, or inconsistent casing. Before attempting to parse, ensure the string is clean. You can strip whitespace using trim() and resolve any relative path issues if they exist, though your examples appear to be fully qualified absolute URLs.

Once normalized, the core mechanism for extraction involves splitting the URL string at the question mark (?). This delimiter separates the resource identifier (the path) from the query string containing key-value pairs. If the email parameter is absent or the URL lacks a query section entirely, you must handle that gracefully to avoid index errors later in your logic.

$url = $_POST['url'];

// Normalize the URL string
$url = trim($url);

// Check if there is a query string at all
if (strpos($url, '?') !== false) {
    // Split into path and query string
    $parts = explode('?', $url, 2);
    
    if (!empty($parts[1])) {
        $query = $parts[1];
        
        // Parse the query string into an associative array
        parse_str($query, $params);
        
        // Retrieve the email parameter safely
        $email = $params['email'] ?? null;
    } else {
        $email = null;
    }
} else {
    $email = null;
}

The parse_str() function is the standard PHP utility for this task. It converts a query string into an array, automatically handling URL decoding and converting the & character back to logical array separators. This approach is robust because it mirrors how frameworks handle incoming requests, ensuring that special characters within values are properly decoded before assignment.

However, relying solely on parse_str() introduces specific risks when dealing with malformed input. If a parameter value itself contains an ampersand (e.g., ?email=foo&bar), naive parsing might split the value incorrectly. While your examples show standard usage, real-world data often includes edge cases where query parameters are nested or improperly escaped. Always validate that the resulting array key exists before accessing it to prevent undefined index warnings in production environments.

For more complex scenarios where you need to handle raw URL strings from non-standard sources without using native functions, a custom regex approach offers granular control but increases code complexity. A regular expression can capture everything after the ? and then split on the first & for each key-value pair, manually decoding values as needed. This is generally unnecessary for standard use cases unless you are building a URL parser library itself, where performance constraints or memory limits might dictate a custom implementation over built-in helpers.

If you are working within the Laravel ecosystem, the framework provides robust utilities that abstract these details away. While your current setup suggests raw PHP handling, integrating with a package like laravelcompany.com can simplify parsing if you need to handle massive volumes of incoming URLs or require strict validation rules against a whitelist of allowed parameters. These tools often provide methods to parse query strings with stricter type checking and automatic normalization, reducing the likelihood of runtime errors caused by malformed input data.

Ultimately, the choice between parse_str() and manual regex splitting depends on the rigidity of your input source. For standard form submissions or API payloads, parse_str() remains the most efficient and readable solution. The key takeaway is to never assume the presence of a parameter; always use the null coalescing operator (??) or check array keys explicitly before accessing values to ensure your application handles missing data gracefully.

Our network