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 ) );
This small fragment would return you:
Array
(
[scheme] => http
[host] => urldosite.u
[path] => /home
[fragment] => 6
)
You need the fragment index. If the other information does not need to be used in any other way, you can enter as a strong argument to function the PHP_URL_FRAGMENT native constant.
In this way, only the fragment value will be returned. In the example, number 6.
In this particular case, if you do not have the desired part in the input URL, you would get a NULL , which does not happen without the second argument because at least the scheme and host .
Unless the URL is poorly constructed, in which case the function would return FALSE .
On request from the topic author, a slightly more extended example. Look what a bozinho 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 do not have much information on how parse_url () considers a URL to be badly worded. That's the only reason I've made conditions instead of one.