Get fragment (value after hash '#') from url in php

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

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

+76
url php anchor
Feb 23 '10 at 11:03
source share
9 answers

If you want to get the value after the hash tag or binding, as shown in the userโ€™s browser: this is not possible using the โ€œstandardโ€ HTTP, because this value is never sent to the server (therefore, it will not be available in $_SERVER["REQUEST_URI"] or similar predefined variables). You will need some kind of client-side JavaScript magic, for example. to include this value as a POST parameter.

If it comes only to parsing a known URL from any source, mck89's answer works fine.

+98
Feb 23 '10 at 11:05
source share

This part is called a "fragment", and you can get it as follows:

 $url=parse_url("http://domain.com/site/gallery/1#photo45 "); echo $url["fragment"]; //This variable contains the fragment 
+35
Feb 23 '10 at 11:05
source share

A) already have C # hash url in PHP? Easily! Just analyze it!

 if( strpos( $url, "#" ) === false ) echo "NO HASH !"; else echo "HASH IS: #".explode( "#", $url )[1]; // arrays are indexed from 0 

Or in the "old" PHP you must first store the exploded ones to access the array:

 $exploded_url = explode( "#", $url ); $exploded_url[1]; 

B) Do you want to get #hash by submitting the form in PHP? => Use some JavaScript MAGIC! (For pre-processing the form)

 var forms = document.getElementsByTagName('form'); //get all forms on the site for(var i=0; i<forms.length;i++) forms[i].addEventListener('submit', //to each form... function(){ //add a submit pre-processing function that will: var hidden = document.createElement("input"); //create an extra input element hidden.setAttribute('type','hidden'); //set it to hidden so it doesn't break view hidden.setAttribute('name','fragment'); //set a name to get by it in PHP hidden.setAttribute('value',window.location.hash); //set a value of #HASH this.appendChild(hidden); //append it to the current form }); 

Depending on your form method attribute, you get this hash in PHP:
$_GET['fragment'] or $_POST['fragment']

Possible returns: 1. "" [empty line] (without a hash) 2. the entire hash INCLUDES the # [hash] sign (because we used window.location.hash in JavaScript, which works just like that :))

C) Do you want to get #hash in PHP JUST from the requested URL?

& nbsp YOU CAN'T!

... (not considering regular HTTP requests) ...

... Hope this helps :)

+24
Dec 09 '14 at 1:48
source share

I was looking for a workaround for this a bit, and the only thing I found was to use URL rewrites to read "anchors". I found in apache docs here http://httpd.apache.org/docs/2.2/rewrite/advanced.html the following ...

By default, redirecting to an HTML anchor does not work, because mod_rewrite avoids the # character, turning it into% 23. This, in turn, breaks the redirection.

Decision. Use the [NE] flag in the RewriteRule. NE means No Escape.

Discussion: this method, of course, will work with other special characters that mod_rewrite, by default, URL encodes.

It may have other reservations, and what may not ... but I think itโ€™s possible to at least do something C # on the server.

+8
Nov 21 '12 at 19:48
source share

You cannot get text after label . It is not sent to the server in the request.

+3
Feb 23 '10 at 11:05
source share

I found this trick, if I insist if I want a value with php .. separate the binding value (#) and get it using javascript and then save it as a cookie, then get the cookie value using php ~

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

+3
Dec 14 2018-11-11T00:
source share

First you need to parse the url, so it looks like this:

 $url = "https://www.example.com/profile#picture"; $fragment = parse_url($url,PHP_URL_FRAGMENT); //this variable holds the value - 'picture' 

If you need to parse the actual URL of the current browser, you need to request a server call.

 $url = $_SERVER["REQUEST_URI"]; $fragment = parse_url($url,PHP_URL_FRAGMENT); //this variable holds the value - 'picture' 
+1
Sep 07 '18 at 15:18
source share

You can replace the string on the client side and then on the server side. Not a particularly reliable solution, but if you don't find such a quick solution like me, that will be enough, I think.

Client:

 var tempString = stringVal.replace('#', 'hashtag'); 

Server:

 $user_message = $_GET['userMessage']; $user_message = str_replace("hashtag", "#", $user_message); 
0
Dec 13 '16 at 22:51
source share

Retrieving data after the hashmark in the query string is simple. Here is an example used when a customer accesses a glossary of terms from a book. It takes the name of the anchor delivered (#tesla), and delivers the term to the client and highlights the term and its description in blue, so it is easy to see.

but. adjust your lines with div id, so the name anchor goes where it is supposed to, and javascript can change text colors

 <div id="tesla">Tesla</div> <div id="tesla1">An energy company</div> 

C. Use Javascript to do the hard work on the server side inserted in a PHP page, or wherever you are.

 <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> 

C. I run the java function automatically when the page loads.

 <script> $( document ).ready(function() { 

D. get the binding (#tesla) from the URL received by the server

 var myhash1 = $(location).attr('hash'); //myhash1 == #tesla 

E. trim the hash sign from it

 myhash1 = myhash1.substr(1) //myhash1 == tesla 

F. I need to highlight the term and description, so I create a new var

 var myhash2 = '1'; myhash2 = myhash1.concat(myhash2); //myhash2 == tesla1 

G. Now I can manipulate the text color for the term and description

 var elem = document.getElementById(myhash1); elem.style.color = 'blue'; elem = document.getElementById(myhash2); elem.style.color = 'blue'; }); </script> 

N. It works. the client clicks on the client side link (xyz.com # tesla) and matches the term. The term and description are highlighted in blue javascript for quick reading. all other entries remained black ..

-5
Feb 05 '17 at 2:13 on
source share



All Articles