Reputation: 19
$domain_parts = explode('.', preg_replace('/\s+/', '', $_GET['domain']));
$sld = $domain_parts[0];
I am wanting to do the functionality of lines 1 and 2 into 1 line of code.
An example of what $_GET['domain']
provides is google.com
What is the cleanest way to do this in one line.
Upvotes: 0
Views: 2455
Reputation: 57052
You can also do it without regular expression like this
$sld = str_replace(' ','',substr($_GET['domain'],0,strpos($_GET['domain'],'.')));
Upvotes: 0
Reputation: 18430
list($domain_parts) = explode('.', preg_replace('/\s+/', '', $_GET['domain']));
$domain_parts
will contain the first element of the array.
You could get the second element like this:-
list(,$domain_parts) = explode('.', preg_replace('/\s+/', '', $_GET['domain']));
See list() for more details.
Or you could do this:-
$domain_parts = explode('.', preg_replace('/\s+/', '', $_GET['domain']))[0];
if you have PHP >= 5.4
Upvotes: 0
Reputation: 28753
You can try with list
like
list($domain_parts) = explode('.', preg_replace('/\s+/', '', $_GET['domain']));
It will directly returns the $domain_parts[0]
.You can also try with strtok
like
echo strtok(preg_replace('/\s+/', '', $_GET['domain']), '.');
See this STRTOK
Upvotes: 2