Skip to content
PHP Logo

What are some alternative methods to extracting values from URLs when the traditional methods do not work?

When traditional methods of extracting values from URLs, such as using $_GET or parsing the URL string, do not work, an alternative method is to use regular expressions to extract the desired values. Regular expressions allow for more complex pattern matching, making it easier to extract specific values from a URL.

php
$url = "https://www.example.com/page?param1=value1&param2=value2";

// Use regular expressions to extract the values of param1 and param2 from the URL
preg_match('/param1=([^&]+)/', $url, $matches1);
$param1 = $matches1[1];

preg_match('/param2=([^&]+)/', $url, $matches2);
$param2 = $matches2[1];

echo "Param1: " . $param1 . "<br>";
echo "Param2: " . $param2;