0

So here's what my URL looks like

http://localhost/search/s.php?search_query=#hashtag

My first response to this was to use $_GET

$search = htmlspecialchars($_GET['search_query'], ENT_QUOTES, 'UTF-8');

So I'd then end up getting nothing, since I had a # at the end. So I then tried

$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$path = parse_url($url, PHP_URL_PATH);
$pathFragments = explode('/', $path);
$id = end($pathFragments);

I'd echo $id, and yet once again get s.php instead.

So my question is, how can I properly do this? I've seen some posts. But none seem to solve my issue.

I'm just trying to get #hashtag.

  • regex should validate the string as well or not? – Braj Jul 12 '14 at 19:58
  • I don't think the hashtag-part is a part of the `REQUEST-URI` – Max Jul 12 '14 at 19:58
  • No need for validation. I just want to get `#hashtag` from the URL. I'd also be having regular words so it has to be dual functioning. @Braj –  Jul 12 '14 at 19:59
  • @max its not the only way I know to get it to the server is to use javascript and ajax – ArtisticPhoenix Jul 12 '14 at 19:59
  • possible duplicate of [Pound sign (#) not working in PHP](http://stackoverflow.com/questions/7479638/pound-sign-not-working-in-php) – Giacomo1968 Jul 12 '14 at 19:59
  • Well, `parse_url()` should return a `fragment` index that gives you it, but probably only if the parsed URL contains a fragment (something after a hashtag). – Max Jul 12 '14 at 20:00
  • Already tried that post @JakeGould –  Jul 12 '14 at 20:01
  • see this question http://stackoverflow.com/questions/14462218/is-the-url-fragment-identifier-sent-to-the-server and the answer " Fragment identifiers are not sent to the server. The hash fragment is used by the browser to link to elements within the same page. " ergo you must use some other method to send it to the server, for example ajax – ArtisticPhoenix Jul 12 '14 at 20:14
  • If you paste the HTML of the form you are using I can update my answer with the proper jQuery selectors. – ArtisticPhoenix Jul 12 '14 at 20:17

3 Answers3

0

You have to send the hashtag to the backend by making an AJAXrequest, preferrably with jQuery. Any URL in the $_SERVER global variable does not preserve the fragment identifier (the part followed by the # - pound sign).

var hash = window.location.hash;

$.ajax({ ... });
silkfire
  • 24,585
  • 15
  • 82
  • 105
0

with javascript

var url = 'http://localhost/search/s.php?search_query=#hashtag';

$(form).submit(function(e){
var url = field.val();
   if(url != '' ){
        var hashtag = url.split('#')[1];
       $.post(serverPageUrl, { "hashtag" : '#'+hashtag }, function(data){
            window.location.href = url; //redirect to simulate original get request.
       });
  }
   return false;
});
ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
0

Try with Negative Lookahead

(?!=#)#\w+$

Online demo

Use (?!=#)#.*$ if there is any thing other then words after # till the end of line.


Or try using grouping. Get the matched group from index 1.

^[^#]*+(.*)$

Online demo

Sample code:

$re = "/(?!=#)#\\w+$/";
$str = "http://localhost/search/s.php?search_query=#hashtag";

preg_match_all($re, $str, $matches);

Output:

#hashtag
Braj
  • 46,415
  • 5
  • 60
  • 76