There are 2 methods you can use for this. The parse_url method involves breaking down the URL into components using PHP's built-in function, allowing for selective extraction of the scheme, host, and path. This approach provides flexibility, making it suitable for scenarios where specific URL components need to be manipulated. On the other hand, the strtok method utilizes tokenization to swiftly extract the portion of the URL before the first occurrence of a specified delimiter ('?'). This approach is concise and particularly well-suited for straightforward cases where the primary objective is to remove query parameters without the need for detailed manipulation of individual components.
Source code viewer
/** * parse_url Approach: */ // Your original URL $originalUrl = "http://example.com/page?param1=value1¶m2=value2"; // Parse the URL // Reconstruct the URL without query parameters $strippedUrl = $parsedUrl['scheme'] . '://' . $parsedUrl['host'] . $parsedUrl['path']; // Output the result echo $strippedUrl; /** * strtok Approach: */ // Your original URL $originalUrl = "http://example.com/page?param1=value1¶m2=value2"; // Use strtok to get the scheme and host // Output the result echo $strippedUrl;Programming Language: PHP