Alexander
Alexander

Reputation: 2463

php string manipulation

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?

Upvotes: 0

Views: 260

Answers (2)

GolezTrol
GolezTrol

Reputation: 116100

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. ;)

Upvotes: 0

Dan Grossman
Dan Grossman

Reputation: 52372

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'];

Upvotes: 2

Related Questions