0

i have this string:

http://localhost/migo2/photo.php?id=68&a_id=83&p_id=349&type=1#p_id=302

and i would like to get the value of type (which is 1 obviously)

i do a

parse_str("http://localhost/migo2/photo.php?id=68&a_id=83&p_id=349&type=1#p_id=302");

and get

echo $type; -> 1#p_id=302

so i was thinking if i had a function that removed everything on the right side of # and the # itself i think i get what i want. Is this a bad way of doing it?

2
  • explode the string by ? and then by # would be the quickest solution. Commented Feb 7, 2011 at 7:14
  • PHP has built-in functions for getting the query string out of the URL, then parsing the query string. Commented Feb 7, 2011 at 7:15

2 Answers 2

2

PHP has parse_url to parse the URL into components, then call parse_str only on the query string.

$str = "http://localhost/migo2/photo.php?id=68&a_id=83&p_id=349&type=1#p_id=302";
$parts = parse_url($str);
parse_str($parts['query'], $arr);
echo $arr['type'];
Sign up to request clarification or add additional context in comments.

Comments

0

Use the parse-url function and specify PHP_URL_QUERY. That will give you the query part first.

This is the main result when searching for PHP Parse Url btw, which is basically your question. ;)

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.