Forum Moderators: coopster
($subdomain, $domain, $extension) = $urlA =~ /(ww[w2])?\.?(.*?)\.(com|net|co)/i; list($subdomain, $domain, $extension) = explode('.', $urlA); list($url, $subdomain, $domain, $extension) =
preg_split('/(.*)?\.?(.*?)\.(com?|net)/i',
$urlA,
-1,
PREG_SPLIT_DELIM_CAPTURE);
$tmp=explode('.',$url);
$extension=array_pop($tmp);
$domain=array_pop($tmp);
$subdomain=$tmp[0]??'';
$subdomain=$tmp[0]??'www';
$subdomain=implode('.',$tmp);
$tmp=explode('.',$url);
$n=count($tmp);
$extension=$tmp[--$n]??'';
$domain=$tmp[--$n]??'';
$subdomain=$tmp[--$n]??'';
$tmp = explode('.', $url);
$n = count($tmp) - 1;
$extension = $tmp[$n--]) ?: '';
$domain = $tmp[$n--] ?: '';
$subdomain = $tmp[$n--] ?: '';
$tmp = explode('.', $url);
$n = count($tmp) - 1;
$extension = $tmp[$n--]);
$domain = $tmp[$n--];
$subdomain = ($n>0)?$tmp[$n]:'';
I'm worried that updating to 7.x will make something stop working unexpectedly
function getHost($url) {
// if I send www.example.com, the array looks like:
// $tmp[0] = www
// $tmp[1] = example
// $tmp[2] = com
$tmp = explode('.', $url);
// and count($tmp) is 3
$n = count($tmp) - 1;
// so this prints "2", as it should
print "Count: $n\n";
// here's where it gets weird, though. Shouldn't $n-- be looking for $tmp[1] at this point?
$extension = $tmp[$n--] ?: '';
// this prints "1", as it should
print "Count: $n\n";
// And if $n = 1 then $n-- should now be 0
$domain = $tmp[$n--] ?: '';
// this prints "0"
print "Count: $n\n";
$subdomain = ($n == 0) ? $tmp[$n] : '';
print "Subdomain: $subdomain\n";
print "Domain: $domain\n";
print "Extension: $extension";
print "\n\n";
}
getHost('www.example.com');
getHost('example.co');
getHost('example.com'); Shouldn't $n-- be looking for $tmp[1] at this point?
So am I to understand that when I do $extension = $tmp[$n--] ?: '';, it looks for $tmp[$n] and THEN negates $n by 1?
$something=$tmp[$n--]; $something=$tmp[$n];
$n--; Note, at this point it's just for my own education. I think a simpler and faster solution might have been staring us in the face the whole time:
When you put the -- operator "after" the variable, it first take the "actual" value of the variable. And only "after" decrease it.
if you put the -- operator "before" the variable, then the variable is first decreased, "before" being evaluated.