Skip to content

How do I import CSV, it seems like it does not read the CSV properly? #3467

Answered by JenuelDev
JenuelDev asked this question in Q&A
Discussion options

You must be logged in to vote

So What I did was created this function, to determine what delimiter is used in the CSV file.

/**
     * @param string $filePath
     * @param int $checkLines
     * @return string
     */
    public function getCsvDelimiter(string $filePath, int $checkLines = 3): string
    {
        $delimiters = [",", ";", "\t"];
        $default = ",";
        $fileObject = new \SplFileObject($filePath);
        $results = [];
        $counter = 0;
        while ($fileObject->valid() && $counter <= $checkLines) {
            $line = $fileObject->fgets();
            foreach ($delimiters as $delimiter) {
                $fields = explode($delimiter, $line);
                $totalFields = count($fields)…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@JenuelDev
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by patrickbrouwers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants