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.
Post your code where you do the assignment in the variable.
– abfurlan
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.'/';
– flourigh
found something?
– flourigh