0

is there a great way to extract subdomain with php without regex ?

why without regex ?

there are a lot of topic about this, one if them is Find out subdomain using Regular Expression in PHP

the internet says it consumes memory a lot, if there is any consideration or you think better use regex ( maybe we use a lot of function to get this solution ) please comment below too.

example

static.x.com = 'static'
helloworld.x.com = 'helloworld'
b.static.ak.x.com = 'b.static.ak'
x.com = ''
www.x.com = ''

Thanks for looking in.

Adam Ramadhan

3
  • A regex in a case like this is trivial and by no means does it consume "memory a lot" Commented Mar 17, 2011 at 13:55
  • 1
    Is this really a safe/wise data collection method? for instance: www.bbc.co.uk Commented Mar 17, 2011 at 14:31
  • horatio umm yes, thats why, im asking this. i my self use a .co.id :| Commented Mar 17, 2011 at 16:30

4 Answers 4

3

http://php.net/explode ?

Just split them on the dot? And do some functions?

Or if the last part (x.com) is the same everytime, do a substring on the hostname, stripping of the last part.

The only exception you'll have to make in your handling is the www.x.com (which technically is a subdomain).

$hostname = '....';
$baseHost = 'x.com';

$subdomain = substr($hostname, 0, -strlen($baseHost));
if ($subdomain === 'www') {
  $subdomain = '';
}
Sign up to request clarification or add additional context in comments.

Comments

1

Whoever told you that regexes "consume a lot" was an idiot. Simple regexes are not very cpu/memory-consuming.

However, for your purpose a regex is clearly overkill. You can explode() the string and then take as many elements from the array as you need. However, your last example is really bad. www is a perfectly valid subdomain.

3 Comments

+1 for calling that guy an idiot. A stupid and ignorant idiot.
wew thank you. everyone says that, or maybe there are a lot of cargo cult programmer in the world of www.
regexes CAN potentially use a lot of memory, but it depends on the pattern. To say they 'always' consume is definitely wrong. But it's also wrong to say they don't ever.
1

You can first use parse_url http://www.php.net/manual/de/function.parse-url.php and than explode with . as delimiter on the host http://www.php.net/manual/de/function.explode.php

I would not say it is quicker (just test it), but maybe this solution is better.

Comments

0
function getSubdomain($host) {
    return implode('.', explode('.', $host, -2));
}

explode splits the string on the dot and drops the last two elements. Then implode combines these pieces again using the dot as separator.

1 Comment

@Adam: www.bbc is the subdomain in this case. Technically speaking, this is correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.