开发者

php string manipulation

开发者 https://www.devze.com 2023-02-08 18:40 出处:网络
i have this string: http://localhost/migo2/photo.php?id=68&a_id=83&p_id=349&type=1#p_id=302

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. I开发者_JS百科s this a bad way of doing it?


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消