1

I want to get query string from a url and change it to the string format.

I copied the following code from other related posts on stackoverflow, but it is not working properly for me.

<?php
 $url="www.example.com/user.php?uname=alan&password=2222";
$parse=parse_str($url);
 echo $uname;
 echo $password;

This code returns the second variable $password as expected, but it failed for the first variable $uname

undefined variable: uname 

Is there something missing in the code?

Any help is greatly appriciated!

Thanks.

1
  • You said it yourself: it just parses the query string. That's only the part after the ?. Commented May 24, 2015 at 7:17

1 Answer 1

3

It looks like parse_str does not work quite correctly with your URL. You probably want to use it in conjunction with parse_url

eg:

$url="www.example.com/user.php?uname=alan&password=2222";
$str = parse_url($url, PHP_URL_QUERY);
$parse = parse_str($str);
echo $uname;
echo $password;

https://php.net/manual/en/function.parse-url.php https://php.net/manual/en/function.parse-str.php

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.