There is no single library function that provides you with the same functionality, but you can get a single line:
$str = "www.mysql.com"; echo implode('.', array_slice(explode('.', $str), 0, 2)); // prints "www.mysql" echo implode('.', array_slice(explode('.', $str), -2)); // prints "mysql.com"
It is easy to turn this into a function:
function substring_index($subject, $delim, $count){ if($count < 0){ return implode($delim, array_slice(explode($delim, $subject), $count)); }else{ return implode($delim, array_slice(explode($delim, $subject), 0, $count)); } }
source share