strip_fragment_from_url()
Removes the fragment (anchor, hash, #fragment) from the URL. The URL must be passed with the protocol.
When you just need to remove the fragment #fragment from the string (URL) without checking whether there is a protocol or not, you can use one of the following codes. Moreover, they are 5 times faster.
$link = 'page/sub-page#fragment';
// speed on 50000 repetitions
echo strip_fragment_from_url($link); // 0.060 sec
echo preg_replace('~#.*~', '', $link); // 0.014 sec
echo str_replace('#fragment', '', $link ); // 0.010 sec
echo substr($link, 0, strpos($link, '#') ); // 0.007 sec
// each line will output to the screen:
// page/sub-page
1 time — 0.000035 sec (very fast) | 50000 times — 0.20 sec (very fast)
No Hooks.
Returns
String. URL without hash.
Usage
strip_fragment_from_url( $url );
- $url(string) (required)
- URL from which the fragment needs to be removed.
Examples
#1 Remove the hash (fragment, anchor) from the URL.
echo strip_fragment_from_url ( 'http://wp-kama.ru/foo#fooooo' ); // returns: http://wp-kama.ru/foo echo strip_fragment_from_url ( 'http://wp-kama.ru/foo?foo=bar#fooooo' ); // returns: http://wp-kama.ru/foo?foo=bar echo strip_fragment_from_url ( '/foo#fooooo' ); // will not process and returns as it was: /foo#fooooo
Changelog
| Since 4.4.0 | Introduced. |