Skip to content
Advertisement

How to extract a specific parameter in a URL?

I’m trying to only strip out a specific parameter from a URL, but I can only do it at its base form. If the script I use adds additional parameter, then that messes up my output. Here is what I currently have

$str = str_replace("https://www.example.com/page/?id[]=", "","$baseUrl"); 

This works perfectly if the $baseUrl = https://www.example.com/page/?id[]=

However, the $baseURL can equal anything. A few examples are

https://www.example.com/page/?id[]=theid&size=50gb&date=11-15
https://www.example.com/page/?size=20gb&id[]=theid&os=windows10
https://www.example.com/page/?type=software&id[]=theid&size=50gb
https://www.example.com/page/?cost=1000&size=50gb&type=software&id[]=theid

How can I extract the value in just “id[]=” ?

Advertisement

Answer

<?php 

// Inintialize URL to the variable 
$url = 'http://www.change.org/register?name[]=Joe&email=joe2020@gmail.com'; 
    
$url_components = parse_url($url); 

parse_str($url_components['query'], $params); 

echo ' Hi '.$params['name'][0].' your emailID is '.$params['email']; 

?>
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement