Get fragment (value after hash '#') from a

2018-12-31 08:34发布

How can I get the fragment (value after hash '#') from a URL in php?

Say from http://domain.com/site/gallery/1#photo45 I want photo45

标签: php url anchor
9条回答
余生无你
2楼-- · 2018-12-31 09:18

That part is called "fragment" and you can get it in this way:

$url=parse_url("http://domain.com/site/gallery/1#photo45 ");
echo $url["fragment"]; //This variable contains the fragment
查看更多
查无此人
3楼-- · 2018-12-31 09:18

I've been searching for a workaround for this for a bit - and the only thing I have found is to use URL rewrites to read the "anchor". I found in the apache docs here http://httpd.apache.org/docs/2.2/rewrite/advanced.html the following...

By default, redirecting to an HTML anchor doesn't work, because mod_rewrite escapes the # character, turning it into %23. This, in turn, breaks the redirection.

Solution: Use the [NE] flag on the RewriteRule. NE stands for No Escape.

Discussion: This technique will of course also work with other special characters that mod_rewrite, by default, URL-encodes.

It may have other caveats and what not ... but I think that at least doing something with the # on the server is possible.

查看更多
心情的温度
4楼-- · 2018-12-31 09:21

I found this trick, if u insist want the value with php.. split the anchor (#) value and get it with javascript, then store as cookie, after that get the cookie value with php~

http://www.stoimen.com/blog/2009/04/15/read-the-anchor-part-of-the-url-with-php/

查看更多
登录 后发表回答