Is there a PHP function for retrieving the "first part" of a URL, similar to how dirname/basename act on file paths?
Something along the lines of
echo "url_basename('example.com/this_post/12312')"
which would return
example.com
parse_url should do this reliably.
Here is the code to output example.com with your given URL:
$url = 'example.com/this_post/12312';
if (strpos($url,'http') === FALSE) {
$url = 'http://'.$url;
}
$arrURLParts = parse_url($url);
$host = $arrURLParts['host'];
echo $host;
Warning: if you omit the part to ensure the URL starts with http then parse_url would return an empty host and put 'example.com/this_post/12312' into $arrURLParts['path'] instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With