Well, it looks like you know for sure that your separator will be "," or ";". This is a good place to start. Thus, you can try to replace all commas (,) with semicolons (;), and then explode with only a semicolon. However, in this case, you will definitely have a problem in some cases, because some lines of your CSV files may be like this:
"name, value", another name, another value, last name, last value
Thus, the delimiter of your CSV file will be a comma if there are four columns in your CSV file. However, changing the commas to semicolons will give you five columns that would be incorrect. So changing a separator to another is not a good way.
But still, if your CSV file is formatted correctly, you can find the correct separator in any of the lines. Thus, you can try to create some function like find_delimiter ($ csvLine) proposed by @johnkork, but the problem is that the function itself cannot know which separator to look for. However, you know exactly all the possible separators, so you can try to create another, very similar function, such as delimiter_exists ($ csvLine, $ delimiter), which returns true or false.
But even the delimiter_exists function ($ csvLine, $ delimiter) is not enough. What for? Because for the CSV example above, you get this, both "," and ";" are the delimiters that exist. For a comma, this is a CSV file with four columns, and for a semicolon, two columns.
Thus, there is no universal way that would give you exactly what you want. However, there may be another way to check - the first line of the CSV file, which is the header, if your CSV files have a header. Basically, headers in a CSV file have (optionally) no characters other than alphanumeric column names, which are limited to a specific delimiter. Thus, you can try to create a function such as delimiter_exists ($ csvHeader, $ delimiter), the implementation of which may be like this:
function delimiter_exists($csvHeader, $delimiter) { return (bool)preg_match("/$delimiter/", $csvHeader); }
For a specific case, you can use it as follows:
$csvHeader = "abc;def"; $delimiter = delimiter_exists($csvHeader, ',') ? ',' : ';';
Hope this helps!