0

i want to get the string after # tag in url with php. but there is no function to find it. so i get it by the javascript on the same page and call that variable into php by

  <script>
  var query = location.href.split('#');
  var a =  String(query[1]);
  </script>          

  <?php 
     $variable = "<script>document.write(a)</script>"; 
     print_r (explode("%",$variable));
  ?>  

but this code print the string as it is and it doesn't split by %.

gives the result like Array ( [0] => z%a ) for this link http://immersed.in/CodeIgniter/#z%a

please help what to do

Jenis Patel
  • 1,617
  • 1
  • 13
  • 20
Meet Vora
  • 1
  • 1
  • 5
  • 1
    You're looking for `explode('#', $url);` – Daan Mar 16 '15 at 12:15
  • @Daan but it's not actually explode the url. if i check with storing manual static link then it explodes but if i run above code.. it gives whole string as it is – Meet Vora Mar 16 '15 at 12:20

2 Answers2

0

"but this code prints the string as it is and it doesn't split by %.?"

<?php 
     $variable = "<script>document.write(a)</script>"; 
     print_r (explode("%",$variable));
 ?> 

Yes the compiler (interpreter) is right!, because when you say $variable = "<script>document.write(a)</script>"; the variable $variable is a string variable because you have <script></script> does not mean that it will be execute as js script for two reasons:

  1. Your script is under quote
  2. Even though you put it out of quote it wont work because you are on the server side ( on php code..) it will not execute till your code comes to the client side ( browser)

Also trying this wont work:
explode('#', $url);

because url part after the # is not sent to the server. here

Also it might be helpful to read about client side vs server side langs.

Community
  • 1
  • 1
dsharew
  • 10,377
  • 6
  • 49
  • 75
  • is there any php function to get whole URL.. i didn't find any so use javascript to get hashtag value – Meet Vora Mar 16 '15 at 12:25
  • Undefined variable: url. what php function to get url ? so i can use it to store in $url – Meet Vora Mar 16 '15 at 12:31
  • oh you have to define the `$url` variable here http://stackoverflow.com/questions/6768793/get-the-full-url-in-php – dsharew Mar 16 '15 at 12:40
  • but any of php function will not return url after # tag. is there any? – Meet Vora Mar 16 '15 at 12:49
  • use the link I gave you to get the url and then use the function on the answer (explode) – dsharew Mar 16 '15 at 13:28
  • sorry to asking help again. but i write this http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI] and it don't send me the value after # tag. also there is a comment "This function does not include username:password from a full URL or the fragment (hash)." – Meet Vora Mar 17 '15 at 03:09
0

You cannot get hash tag using php. And javascript will run on client side and php will run on server side. Refer to this links. Similar answer : Getting FULL URL with #tag,

Community
  • 1
  • 1
arshovon
  • 13,270
  • 9
  • 51
  • 69
  • yes.. that i know and that's why i use javascript to get hash value and store in to php as above mentioned code.. but question is how can i explode php variable? – Meet Vora Mar 16 '15 at 12:42