Are there any potential pitfalls when using the explode function in PHP to extract text before a specific character?

One potential pitfall when using the explode function in PHP to extract text before a specific character is that if the character is not found in the string, the function will return an array with only one element containing the original string. To solve this issue, you can check if the character exists in the string before using explode.

$string = "Hello, World!";
$delimiter = ",";
if (strpos($string, $delimiter) !== false) {
    $beforeDelimiter = explode($delimiter, $string)[0];
    echo $beforeDelimiter;
} else {
    echo "Delimiter not found in string.";
}