How to get parameters from a URL string in PHP?

PHPServer Side ProgrammingProgramming

To get parameters from a URL string in PHP, the code is as follows−

Example

 Live Demo

<?php
   $url = 'http://www.example.com/register?name=demo&email=example12@domain.com';
   $res = parse_url($url);
   parse_str($res['query'], $params);
   echo 'Email = '.$params['email'];
?>

Output

This will produce the following output−

Email = example12@domain.com

Example

Let us now see another example −

 Live Demo

<?php
   $url = 'http://www.example.com/register?name=demo&email=example12@domain.com';
   $res = parse_url($url);
   parse_str($res['query'], $params);
   echo 'Email = '.$params['name'];
?>

Output

This will produce the following output−

Email = demo
raja
Published on 27-Dec-2019 12:12:54
Advertisements