3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function get_last_dot_occurence ($url ) { $fullArray = explode('.', $url); $newArray = ""; switch (count($fullArray)) { case 0: break; case 1: $newArray = $url; break; case 2: $newArray = array("string1" => $fullArray[0], "string2" => $fullArray[1]); break; default: $string1 = ""; for ($i=0; $i < count($fullArray)-2; $i++) { $string1 .= $fullArray[$i]; if($i != count($fullArray)-3) $string1 .= '.'; } $string2 = $fullArray[count($fullArray)-2].'.'.$fullArray[count($fullArray)-1]; $newArray = array("string1" => $string1, "string2" => $string2); break; } return $newArray; } print_r(get_last_dot_occurence("abc.com")); print_r(get_last_dot_occurence("abc.name.com")); print_r(get_last_dot_occurence("abc.xyz.name.org"));
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
Array ( [string1] => abc [string2] => com ) Array ( [string1] => abc [string2] => name.com ) Array ( [string1] => abc.xyz [string2] => name.org )