Is it recommended to handle URL parsing in PHP or in the database directly?

Handling URL parsing in PHP is generally recommended as it allows for more flexibility and control over the parsing process. PHP provides built-in functions like parse_url() and parse_str() that make URL parsing straightforward. This approach also keeps the logic separate from the database, making the code more maintainable and easier to debug.

$url = "https://www.example.com/path/to/page?param1=value1&param2=value2";
$parsed_url = parse_url($url);
$query_params = [];
if(isset($parsed_url['query'])){
    parse_str($parsed_url['query'], $query_params);
}

// Accessing parts of the parsed URL
echo $parsed_url['scheme']; // Output: https
echo $parsed_url['host']; // Output: www.example.com
echo $parsed_url['path']; // Output: /path/to/page

// Accessing query parameters
echo $query_params['param1']; // Output: value1
echo $query_params['param2']; // Output: value2