0

Data from CSV like below, just one column

| BeKwX8iN3wzHDvCaxBD1 |
| 3rPB9t6EF3RGla28YbLE |
| OAYRwbrkctcVbrXaaTef |
| N8lxYdvx47FI7eYt5FUX |
| zwtRdHr3aYYnX9avcMjX |
.....

file content look: https://drive.google.com/a/hiiir.com/file/d/0B4ZVHStLEPq3bklUUHkzbDN1MkE/view?usp=sharing

The code

$row = 1;
    if (($handle = fopen($serial['tmp_name'], "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $num = count($data);
            echo "<p> $num fields in line $row: <br /></p>\n";
            $row++;
            for ($c=0; $c < $num; $c++) {
                echo $data[$c] . "<br />\n";
            }
        }
        fclose($handle);
    }

the issue is that when I print $num, I got int(1), because they are all in one array()

array(1) {
  [0]=>
  "BeKwX8iN3wzHDvCaxBD1
  3rPB9t6EF3RGla28YbLE
  OAYRwbrkctcVbrXaaTef
  N8lxYdvx47FI7eYt5FUX
  zwtRdHr3aYYnX9avcMjX"}

I know fgetcsv() controll by $delimiter, and this case is ,,but it not suitable for my case. Is it possible to separate by 20 Characters?

2
  • 3
    Seems no real CSV so why using fgetcsv if each line of the file looks like |abc| then do array_map(function($a){ return trim($a,'|');},file($serial['tmp_name'])); to get the data in a array (one entry for reach line) or use fgets() instead of fgetcsv(). Commented Mar 9, 2017 at 10:55
  • 1
    And you can change the $delimiter to everthing you need, read more: php.net/manual/en/function.fgetcsv.php Commented Mar 9, 2017 at 10:59

1 Answer 1

1

You cannot use fgetcsv to read a fixed length CSV, you have to do it on you own.

[EDIT] The problem here was not a CSV problem, it was caused by end line "\r". See comments below to deal with it. [/EDIT]

In your case, use the delimiter params :

$data = fgetcsv($handle, 1000, "|")

Your code exampled :

<?php

if (($handle = fopen('csv.txt', "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, "|")) !== FALSE) {
        array_pop($data);
        array_shift($data);
        var_dump($data);
    }
    fclose($handle);
}

Result :

array(1) {
  [0]=>
  string(22) " BeKwX8iN3wzHDvCaxBD1 "
}
array(1) {
  [0]=>
  string(22) " 3rPB9t6EF3RGla28YbLE "
}
array(1) {
  [0]=>
  string(22) " OAYRwbrkctcVbrXaaTef "
}
array(1) {
  [0]=>
  string(22) " N8lxYdvx47FI7eYt5FUX "
}
array(1) {
  [0]=>
  string(22) " zwtRdHr3aYYnX9avcMjX "
}
Sign up to request clarification or add additional context in comments.

5 Comments

Hi, | still not work, is the source file format problem?
I downloaded you file. It's Apple new line "\r". This is the problem here. You get 2 options : either use ini_set('auto_detect_line_endings', true); in your script or first read the entire file and transform \r to \n. EDIT : If your cell delimiter is ,, forget about the |
THX! ini_set('auto_detect_line_endings', true) it works!
If I want to transform \n to \r , I try to set $handle = preg_replace("\n","\r",$handle); before while loop, but I got Warning preg_replace(): Delimiter must not be alphanumeric or backslash
preg_replace is a regex based function. As the error says, it must not be alphanum or \. I usually use / (or # when I have / in pattern) : preg_replace('/\n/', "\r", fgets($handle)) is what you want. For your need, str_replace("\n", "\r", fgets($handle)) would do the trick.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.