3

I have a CSV file like below

マテル・インターナショナル株式会社,dog
株式会社タカラトミーアーツ,apple
大網株式会社,banana

I have used SplFileObject to get CSV ,

$file = new SplFileObject(my_path);
$file->setFlags(SplFileObject::READ_CSV  | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY);

foreach ($file as $key => $row) {
    print_r($row);
}

I have gotten below output in console, My script will run in shell not in browser.

Array
(
    [0] => マテル・インターナショナル株式会社
    [1] => dog
)

Array
(
    [0] => 株式会社タカラトミーアーツ,apple
)

Array
(
    [0] => 大網株式会社
    [1] => banana
)

I am trying in console not in browser.

Here for 1st line Array is okay but for 2nd line data showing in array single element. All of lines contain a comma.How I can solve this issue for encoding character ?

Niloy Rony
  • 602
  • 1
  • 8
  • 23
  • 1
    Are you sure that there are no spaces between *,* and apple or the previous encoded element that you want to split? – Tushar Sep 29 '20 at 13:26
  • 1
    Yes I am sure , there has not spaces between, there has only comma. – Niloy Rony Sep 29 '20 at 13:27
  • 1
    have you tried with the data in the opposite columns? ie. `apple,株式会社タカラトミーアーツ` instead of `株式会社タカラトミーアーツ,apple` – JoSSte Sep 29 '20 at 13:27
  • @JoSSte I have tried and it's working fine. – Niloy Rony Sep 29 '20 at 13:28
  • Could you try explode function and verify if it produces the same output? – Tushar Sep 29 '20 at 13:29
  • I tried this and it's working in PHP 7.2. What PHP version are you running? – Rob Ruchte Sep 29 '20 at 13:30
  • @RobRuchte I am using PHP 7.4.6. Would you please share the array ? – Niloy Rony Sep 29 '20 at 13:31
  • Also working for me in 7.4.10 – Rob Ruchte Sep 29 '20 at 13:33
  • @RobRuchte Would you please share your source code as an ans ? Then I can check what I have made mistake. – Niloy Rony Sep 29 '20 at 13:35
  • 1
    I just copied your code verbatim and saved your sample text in a file. What's the file encoding for your file? – Rob Ruchte Sep 29 '20 at 13:41
  • @RobRuchte it's UTF-8 – Niloy Rony Sep 29 '20 at 13:44
  • I also just copied what you have posted and saved it with Unix endings in UTF-8 file and ran it without incident. – JoSSte Sep 29 '20 at 13:49
  • I copied the above 3 lines to a file (UTF-8 encoded) and I was unable to reproduce the problem under PHP 7.3.18. I suspect that the comma on the 2nd line of the original file is not an ASCII character. Replace it. – jspit Sep 29 '20 at 13:59
  • @jspit it's work only if I give a space ! I don't know why are you not facing this problem ! Are you using mac ? – Niloy Rony Sep 29 '20 at 14:02
  • No. I've tested it on Linux and Win10 (PHP 7.4). – jspit Sep 29 '20 at 14:04
  • @jspit I got your point please check it in console ! Not in browser. I am trying this code in console. – Niloy Rony Sep 29 '20 at 14:07
  • @RobRuchte My script in shell command not in browser. You will get my same result if you open it using git bash. – Niloy Rony Sep 29 '20 at 14:11
  • If you quote the first column (`"株式会社タカラトミーアーツ"`), does that resolve it? Also, are you suggesting that locally it works if you run it as a web request and it's only console that has the issue? – Jared Farrish Sep 29 '20 at 14:13
  • @JaredFarrish after given double quote I am facing same issue. It's working fine in browser but not in console. – Niloy Rony Sep 29 '20 at 14:17
  • 2
    I'm running in a Bash shell on MacOS. Have you tried running this in another environment? A quick Google search for "git bash UTF-8 file encoding problems" returns some results you should probably check out. This is almost certainly a file encoding issue, and the most likely culprit is your weird shell. Your code is fine. – Rob Ruchte Sep 29 '20 at 14:18
  • It has to involve something reading the file incorrectly. How it gets confused and doesn't see/parse the comma due to that seems confusing to me, though. The online/offline, web/console difference seems like a clue. – Jared Farrish Sep 29 '20 at 14:29
  • @RobRuchte It's working fine in linux ! Thanks for your advice ! – Niloy Rony Sep 29 '20 at 14:34

0 Answers0