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
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
You need to parse the url first, so it goes like this:
If you need to parse the actual url of the current browser, you need to request to call the server.
Getting the data after the hashmark in a query string is simple. Here is an example used for when a client accesses a glossary of terms from a book. It takes the name anchor delivered (#tesla), and delivers the client to that term and highlights the term and its description in blue so its easy to see.
A. setup your strings with a div id, so the name anchor goes where its supposed to and the javascript can change the text colors
B. Use Javascript to do the heavy work, on the server side, inserted in your PHP page, or wherever..
C. I am launching the java function automatically when the page is loaded.
D. get the anchor (#tesla) from the url received by the server
E. trim the hash sign off of it
F. I need to highlight the term and the description so i create a new var
G. Now I can manipulate the text color for the term and description
H. This works. client clicks link on client side (xyz.com#tesla) and goes right to the term. the term and the description are highlighted in blue by javascript for quick reading .. all other entries left in black..
You can't get the text after the hash mark. It is not sent to the server in a request.
You could do a string replace on the client side then the server side. Not a particularly robust solution but if you wan't a quick solution like me it will suffice I think.
Client:
Server:
If you want to get the value after the hash mark or anchor as shown in a user's browser: This isn't possible with "standard" HTTP as this value is never sent to the server (hence it won't be available in
$_SERVER["REQUEST_URI"]
or similar predefined variables). You would need some sort of JavaScript magic on the client side, e.g. to include this value as a POST parameter.If it's only about parsing a known URL from whatever source, the answer by mck89 is perfectly fine though.
A) already have url with #hash in PHP? Easy! Just parse it out !
Or in "old" PHP you must pre-store the exploded to access the array:
B) You want to get a #hash by sending a form to PHP?
=> Use some JavaScript MAGIC! (To pre-process the form)
Depending on your
form
'smethod
attribute you get this hash in PHP by:$_GET['fragment']
or$_POST['fragment']
Possible returns: 1.
""
[empty string] (no hash) 2. whole hash INCLUDING the#
[hash] sign (because we've used thewindow.location.hash
in JavaScript which just works that way :) )C) You want to get the #hash in PHP JUST from requested URL?
YOU CAN'T !
...(not while considering regular HTTP requests)...
...Hope this helped :)