// Encode a string for use in a URL using the urlencode() function
$url = 'https://www.example.com/page.php?name=John Doe';
$encoded_url = urlencode($url);
echo $encoded_url . '<br>';
// Output: 'https%3A%2F%2Fwww.example.com%2Fpage.php%3Fname%3DJohn+Doe'

// Decode a URL encoded with urlencode()
$decoded_url = urldecode($url);
echo $decoded_url;
// Output: 'https://www.example.com/page.php?name=John Doe'
urldecode(string $string): string
// Original string
$search = 'hello world!';

// Encode with urlencode()
$encodedSearch = urlencode($search);

// Build the URL
$url = 'https://example.com/search.php?q=' . $encodedSearch;

echo "Generated URL: $url";
// Output: 'Generated URL: https://example.com/search.php?q=hello+world%21'

// Extract query string from URL
$parsedUrl = parse_url($url);
$queryString = $parsedUrl['query']; // 'q=hello+world%21'

// Manually extract value from query string (without using parse_str)
$queryParts = explode('=', $queryString);
$encodedValue = $queryParts[1]; // hello+world%21

// Explicitly decode using urldecode()
$decodedValue = urldecode($encodedValue);

echo "Decoded search term (using urldecode): " . $decodedValue;
// Output: 'Decoded search term (using urldecode): hello world!'
// Encoded query string retrieved from a database or logs (includes English + encoded spaces only)
$encodedQuery = 'name=John+Doe&city=New+York';

// Split each query parameter pair
$pairs = explode('&', $encodedQuery);
$params = [];

foreach ($pairs as $pair) {
    $parts = explode('=', $pair);
    $key = $parts[0];
    $value = isset($parts[1]) ? $parts[1] : '';
    // Decode each value explicitly using urldecode()
    $params[$key] = urldecode($value);
}

echo 'Name: ' . $params['name'] . '<br>';  // Output: 'Name: John Doe'
echo 'City: ' . $params['city'] . '<br>';  // Output: 'City: New York'