-1

I'm struggling with the following. After a user uploads a CSV file, I want to compare that header of the CSV file match content and order. I have tried with several PHP methods, I always have a FALSE result on the first value on my string. So I basically make a string of control an array with explode() and then get the first line of my CSV.

CSV file

value1,value2,value3
My First Value,My Second Value,My Third Value

Code

$controlHeader = explode(',',"value1,value2,value3");
$csvHeader = fgetcsv($file_handle, 2000, ',');

foreach($csvHeader as $key => $header){
    if (strpos($controlHeader[$key],$header) === false){
        echo('false');
        } else {
        echo 'true';
        }
}

Is there any reason why my First value in CSV line is always considered as different than the control one? I also try with array_diff() it always reports the key [0] as different. Of course, My CSV header is good, trust me I've triple check that. WHat I'm missing here?

OptimusCrime
  • 14,662
  • 13
  • 58
  • 96
Benoit
  • 374
  • 4
  • 20
  • 2
    What is the content of the CSV file? Please include the related lines. We are unable to guess what is wrong, and we are not psychic. – OptimusCrime Sep 15 '17 at 13:35
  • CSV file contains basic text separated by COmma. Whatever the value here, the first value of my `$controlHeader`string always return true. – Benoit Sep 15 '17 at 13:44
  • It seems likely that the problem is related to the specific value in that position, so using generic terms like `value1` may actually be obscuring the problem. (Unless those really are the values.) – Don't Panic Sep 15 '17 at 13:45
  • The actual value in that poisition is "18 Digit ID" but whatever i change it to "value", "dummies" etc... sill the same. also in my `if/else`statement` I try with `if ($controlHeader[$key] === $header)`it always return false for the first character. Did the `explode` method can generate that? or the `fgetcsv`? – Benoit Sep 15 '17 at 13:47
  • 1
    I've debugged the code and it works fine: https://3v4l.org/DZa3f . I think you are the victim of encoding or perhaps some hidden characters. – OptimusCrime Sep 15 '17 at 13:49
  • Have you tried to `var_dump($controlHeader[$key],$header)` and verify that they really are what you think they are? That would probably be my first step if I had this problem. – Don't Panic Sep 15 '17 at 13:50
  • Thank for the tips on trakcing the encoding side. It's seems to be a non UTF-8 source file, but a ASCII one. I'll explore that way – Benoit Sep 15 '17 at 13:53

1 Answers1

1

While providing the CSV file would help, but not always. The header line in the file may contain some invisible characters. My bet is that it's a UTF-8 Byte Orde Mark - https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8

Open the file in binary editor and check if the first three bytes are indeed what you expect. If it's UTF-8 signature, strip them before feeding to fgetcsv.

astax
  • 1,769
  • 1
  • 14
  • 23
  • Bingo! There is 3 characters in this position 'Ôªø' So How can I trim the value to get rid of them? Is regex can help? – Benoit Sep 15 '17 at 13:58
  • Yes, this looks like BOM signature. You can read the whole first line of the file using `fgets()`, strip BOM using something like this - https://stackoverflow.com/questions/10290849/how-to-remove-multiple-utf-8-bom-sequences-before-doctype and then parse CSV line with `str_getcsv()` function. – astax Sep 15 '17 at 15:43