Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove .com, .net etc. from URL string

Tags:

php

If $domain = apple.com, or mit.org.edu.au, how can I remove the '.com' and everything starting from the dot using PHP? e.g. apple.com becomes apple, mit.org.edu.au becomes mit (disregard www.).

like image 868
John Grier Avatar asked Jan 04 '13 03:01

John Grier


Video Answer


1 Answers

If you already have the domain then simply explode the domain and take the first element of the array.

$domain = 'apple.com';
$domain_parts = explode('.', $domain);
echo $domain_parts[0]; // returns apple

Note that the above will not account for subdomains. I.e. 'www.apple.com' would return 'www'. But based on what you have asked above, explode may be adequate.

If you don't already have the domain then you can use PHP's parse_url function to extract the domain (host) from the URL.

like image 174
PassKit Avatar answered Oct 07 '22 05:10

PassKit