How can PHP be used to ensure that a specific number of parts are extracted from a string, even if the delimiter appears multiple times within the string?

When extracting parts from a string using a delimiter that appears multiple times within the string, we can use the `explode()` function in PHP to split the string into an array of parts based on the delimiter. To ensure that a specific number of parts are extracted, we can combine `explode()` with `array_slice()` to limit the number of elements in the resulting array.

$string = "apple,banana,cherry,dates,grape";
$delimiter = ",";
$numberOfParts = 3;

$parts = explode($delimiter, $string);
$limitedParts = array_slice($parts, 0, $numberOfParts);

print_r($limitedParts);