What are the best practices for handling overflown elements when using the explode function with a limit parameter in PHP?

When using the explode function in PHP with a limit parameter, it's important to handle the case where the string has more elements than the limit specified. One common approach is to check the number of resulting elements after exploding the string and then concatenate the remaining elements back together.

$string = "apple,banana,orange,grape";
$limit = 2;

$exploded = explode(',', $string, $limit + 1);

if(count($exploded) > $limit) {
    $lastElement = end($exploded);
    array_pop($exploded);
    $exploded[$limit-1] .= ',' . $lastElement;
}

print_r($exploded);