Solution 1:
<?php
$str = 'http://example.abc.com';
// Split the URL into 3 parts: protocol, empty after //, and the rest (host + path)
$last = explode("/", $str, 3);
// Take the host part ("example.abc.com") and split by dot
$ans = explode('.', $last[2]);
echo $ans[0]; // Output: example
?>
✅ Explanation:
explode(“/”, $str, 3) → breaks URL into [“http:”, “”, “example.abc.com”].
Index 2 gives us the domain (example.abc.com).
explode(‘.’, $last[2]) → breaks into [“example”, “abc”, “com”].
Index 0 is the subdomain/host prefix (example).