Capture data after character # in browser URL

Asked

Viewed 2,134 times

2

I have the following URL http://urldosite.u/home#6 that when I move to a variable, it simply cancels out everything after the #. And I really want to pick up just what this after this and this character, in the example, the number 6

I’ve tried so many ways and I got nothing.

$sitehost = $_SERVER['HTTP_HOST']; 
$siteuri = urldecode($_SERVER['REQUEST_URI']); 
$sitescript = $_SERVER['SCRIPT_NAME']; 
$siteparametro = $_SERVER['QUERY_STRING']; 
$siteprotocolo = (strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') === false) ? 'http' : 'https'; $siteurl = $siteprotocolo.'://'.$sitehost.'/';
  • Post your code where you do the assignment in the variable.

  • This code below works perfectly for everything I have done so far, I had no difficulty doing everything I need, but to capture # in the browser url, it is not working $sitehost = $_SERVER['HTTP_HOST']; $siteuri = urldecode($_SERVER['REQUEST_URI']); $sitescript = $_SERVER['SCRIPT_NAME']; $siteparameter = $_SERVER['QUERY_STRING']; $siteprotocol = (strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') === false) ? http: 'https'; $siteurl = $siteprotocol. ':/'. $sitehost.'/';

  • found something?

2 answers

3

With parse_url() you get this and many other information about a given URL:

<?php

$url = 'http://urldosite.u/home#6';

print '<pre>'; print_r( parse_url( $url ) );

That little fragment would return you:

Array
(
    [scheme] => http
    [host] => urldosite.u
    [path] => /home
    [fragment] => 6
)

You need the index Fragment. If the other information does not need to be used in any other way, you inform as sgeundo argument to function the native constant PHP_URL_FRAGMENT.

Thus, only the value corresponding to the Fragment will be returned. In the example, number 6.

In this particular case, if there is no desired part in the input URL, you would receive a NULL, what does not happen without the second argument because at least the Scheme and the host.

Unless the URL is too poorly constructed, in which case the function would return FALSE.


On request from the author of the topic, a slightly more extended example. Look what a nice face I am :P:

$url = 'http://urldosite.u/home#6';

$fragment = parse_url( $url, PHP_URL_FRAGMENT );

if( $fragment !== FALSE && ! is_null( $fragment ) ) {

    // Faz alguma coisa com $fragment
}

I don’t have much information about how parse_url() considers a URL to be misspelled. That’s the only reason I made conditions instead of one.

  • There’s always a Do Contra to be negative without reading or without even saying why you did it.

  • 1

    I even considered negativizing your answer, but I soon turned back because I hate to negativize someone without having a really important reason for it. However, it would be justifiable because your answer does not relate to the question asked and, I am sorry to say, still proposes a gambit.

  • in this case I put the code exactly as follows <? php $url = 'http://localhost/home#6'; print '<pre>'; print_r(parse_url($url)); ? > , but how I capture the URL that is there in the browser without eating the character #?

  • That’s basic programming, buddy. print_r() is a debugging function, not to be used in a real case. You will assign parse_url() return to a variable before you can work with it. ;)

  • Man, if it’s the basics, then I admit I’m below the basics, really, if it’s not too much trouble. Could you explain it to me? because I’ve done a lot of things in the script and the damn # character doesn’t appear at all, only if I write it manually.

  • 2

    The character # won’t show up. Without a second argument, parse_url() will parse the URL and return each component separately. With it, it will return a string or an integer ready for you to use. The character # is not part of the component. It is a... "delimiter", which the function takes as a basis to separate the path (in your case, the expression home) of the fragment itself.

Show 1 more comment

1


  • this is exactly what I’m looking for, I want to send to a variable what has after the hash, IE, undo everything before home#123456789 and get only the 123456789, even typing anything before the hash, with js would be possible? I will search on the link, very grateful

  • As I said, PHP does not receive the hash (unfortunately), ONLY with JS can you capture - just watch Gmail, Twitter and others. The link has only one example, perhaps too superficial, but serves as a basis

  • Read(m) carefully my answer that you(s) will(will) see that it is possible to YES get the value of a hash with PHP. You won’t be able to catch the hash symbol (#), but this is solved with a simple concatenation.

  • but you are giving the url $url = http://urldosite.u/home#6 and not by far what he asked for... he wants to capture the current url of the browser

  • exactly as Papacharlie said, in his case @Brunoaugusto , I have to enter the URL manually, and I want to take the browser and stick in the variable. in the case I saw how it does in JS now I have to pass the js variable to the php variable, thanks to all.

  • 1

    Do not forget to give the question as resolved.

  • But that was not said in the original topic. With what was presented, I developed my response assuming that the URL was manually informed to the program. At no time was it said that the URL to be analyzed should be the one visible in the browser. Dice! Dice! I need dice! I can’t make bricks without clay!

  • True, but I could tell by the first comment where he uses $_SERVER['REQUEST_URI'] - I figured I wanted to capture the browser url

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.