I'm trying to extract the last segment of this URL: http://test.com/blog/#segmentIwant (the #segmentIwant is the string I want). The hash tag is generated from a link within the website. Any help is much appreciated!
Asked
Active
Viewed 2,646 times
2 Answers
5
PHP cannot read the hash, the server doesnt even recieve it at all!

Zevi Sternlicht
- 5,399
- 19
- 31
-
-
@user2069381 sure, with pure javascript use, `window.location.hash;` – Zevi Sternlicht Jul 10 '13 at 18:48
1
Well while you load a page with # tag, php does not know it as # tags on url are treated by basically javascript and are not sent to server. But with a simple AJAX request you can send the value to server on loading the page and get the response through that AJAX response.
To grab the url with Javascript:
var url = document.URL;
And try basename() function to get the last part of the sent url with PHP code.
echo basename( "http://test.com/blog/#segmentIwant" );
Or a simple function in javascript to do it:
var url = document.URL;
hashed_string = url.split("#");
alert(hashed_string[1]);

Fallen
- 4,435
- 2
- 26
- 46
-
1Please do a favor while voting -1, post a comment too to help the poster avoid the reason of -1 next time. – Fallen Jul 10 '13 at 18:43
-
1Wasnt me who downvoted but I presume that it is because PHP cannot read the HASH at all! – Zevi Sternlicht Jul 10 '13 at 18:51
-
@InGodITrust: yes, I know. but my solution was based on a string. And we can still pass the url to a php script via ajax if we want. Technically there is nothing wrong, I believe. Please correct me if I'm wrong. Thanks :) – Fallen Jul 10 '13 at 18:53
-
AJAX wouldnt help you, because the receieving PHP script still wouldnt know about the HASH unless you put in a `$_GET` variable – Zevi Sternlicht Jul 10 '13 at 18:56
-
@InGodITrust sorry I think you didn't get me. I'd send the url I grabbed with JS with `$_GET` or `$_POST` of course. Otherwise why I'll create an ajax request, I wonder :) – Fallen Jul 10 '13 at 18:57
-