Using strstr ()
$url = strstr($url, '#', true);
Using strtok ()
A shorter way using strtok :
$url = strtok($url, "#");
Using explode ()
An alternative way to separate the URL from the hash:
list ($url, $hash) = explode('#', $url, 2);
If you don't want $hash , you can omit it in list :
list ($url) = explode('#', $url);
With PHP version> = 5.4 you donβt even need to use list :
$url = explode('#', $url)[0];
Using preg_replace ()
Mandatory regular expression:
$url = preg_replace('/#.*/', '', $url);
Using Purl
Purl is a neat URL manipulation library:
$url = \Purl\Url::parse($url)->set('fragment', '')->getUrl();
arnaud576875
source share