2

I have a piece of code that reads in values from a CSV file, all working grand and then each individual record is validated and used in an API call to an external source, all of this is working grand.

Now today I have a CSV file uploaded that when I open it it looks to have 3 spaces at the end of each entry in the CSV which causes the API call to fail.

I've tried using trim() but it doesn't do anything, I've also tried using preg_replace('/^((?=^)(\s*))|((\s*)(?>$))/si', '', $pValue) and this isn't having any affect either.

I opened the CSV file in Notepad++ and enabled it to show all symbols, there are clearly 3 spaces and the CR and LF.

Has anyone come across an issue like this before, code snippets below and an example on codepad.

EDIT:

Example: http://codepad.org/DfjzPcUU

Code snippets, if more is needed please let me know.

$vCSVData = self::getCSVData($vPendingFilePath);

foreach ($vCSVData AS $vKey => $vValue)
{
     echo self::getNubmer($vValue);
     ...
}

getCSVData():

private function getCSVData($pFilePath)
{
    return call_user_func_array('array_merge', array_map('str_getcsv', file($pFilePath, FILE_SKIP_EMPTY_LINES)));
}

CSV File Snippet:

Blue Table   
Green Chair  
Temp. Table   
llanato
  • 2,508
  • 6
  • 37
  • 59
  • "I've tried using trim() but it doesn't do anything" - sounds like a problem with your code. – Sam Dufel Aug 26 '14 at 20:44
  • We have to be able to reproduce the problem. Posting a small sample of the CSV would make it easier to do this. Your code could also show obvious problems (maybe you're trying to `trim` the entire line in a CSV file.) – sjagr Aug 26 '14 at 20:45
  • some times spaces are not spaces, but tabs or one of a number of other characters –  Aug 26 '14 at 20:45
  • @sjagr, I've added bits of the code I'm using and a sample of the CSV, not sure how useful the CSV sample is as I'm not sure it would copy and paste with the same issue onto stack. – llanato Aug 26 '14 at 20:50
  • 2
    You're doing all this because you're parsing a csv?! Use the right functions! http://php.net/fgetcsv. A valid CSV can have the same row span multiple lines - your current method won't work with this. – Mike B Aug 26 '14 at 20:51
  • @MikeB, it's an inherited codebase and it works as currently is. – llanato Aug 26 '14 at 20:53
  • 1
    @atallon If you used trim and it didn't work then you're not using it right or those aren't spaces. Scrap everything in this question regarding csvs, apis, uploads, etc and give us the brass tacks: provide a string with the padding and how trim won't work with it. – Mike B Aug 26 '14 at 20:54
  • [`rtrim()`](http://php.net/manual/en/function.rtrim.php) could be of use. - *"rtrim — Strip whitespace (or other characters) from the end of a string"* – Funk Forty Niner Aug 26 '14 at 20:54
  • @atallon Trim works on the end of a string. Each line can have space before the break and not be taken out with trim if the contents of the CSV are in a single sting. To see if this is the case try `/\s{3}\n|\s{3}$/` – hcoat Aug 26 '14 at 20:57
  • @hcoat Are you sure? http://codepad.org/KHCkC12r – Mike B Aug 26 '14 at 20:59
  • @MikeB, here's an example, and a copy and paste of one of the entries from the CSV, http://codepad.org/DfjzPcUU – llanato Aug 26 '14 at 21:04
  • @MikeB Correct link to example. [http://codepad.org/4cnxfzP2](http://codepad.org/4cnxfzP2) – hcoat Aug 26 '14 at 21:06
  • @hcoat But you're not trimming there.. the last line was trimmed and there's no trailing spaces. The new lines shouldn't be trimmed as their not at the beginning nor end of the string. – Mike B Aug 26 '14 at 21:10
  • @atallon Those are ASCII 194 chars - not spaces. http://codepad.org/ELMuputC – Mike B Aug 26 '14 at 21:11
  • @MikeB That was my point. – hcoat Aug 26 '14 at 21:12
  • @hcoat Why not preg_replace('/\s/', '', $string); to remove all white space then? He's using file() to read in the file so new lines shouldn't be an issue. – Mike B Aug 26 '14 at 21:15
  • @MikeB Only because I don't know if there are other relevant spaces. – hcoat Aug 26 '14 at 21:17
  • @MikeB, Thanks for spotting that, is there an easy way to remove ASCII 194 characters, never come across this myself before? – llanato Aug 26 '14 at 21:19
  • @MikeB `preg_replace('/\s/', '', $string);` Wouldn't that affect the space between the words `Blue Table`? – Funk Forty Niner Aug 26 '14 at 21:20
  • @Fred-ii- I don't know! I don't know what case hcoat is driving at. – Mike B Aug 26 '14 at 21:21
  • That first pattern is seriously wonky. Why check if the start of string anchor is followed by a string of string anchor? Why bother replacing zero whitespaces with an empty string? Why is the end of string match inside an atomic group? Why is there a case-insensitive flag (`i`) if there are no letters to affect? Why is that an `s` flag if there are no dots to affect? Where ever you copied that from, please do not revisit that source. – mickmackusa Sep 10 '22 at 08:10

1 Answers1

8

As it turns out that what I thought was white space was actually ASCII 194 characters (Thanks to MikeB) for figuring this out, as trim, rtrim, preg_replace, str_replace with the normal " " (empty space) check didn't work the below snippet is what worked for me.

preg_replace("/[\xA0\xC2]/", "", "Table   ")

xA0 is char 160 and xC2 is char 194.

Also you can use trim in this instance by using the below statement, using trim over preg_replace is slightly faster for processing time, with that said the overheads in each case and the difference are negligible, in my case either will suffice.

trim("Table   ", "\xA0\xC2")

Example: http://codepad.org/8H5Ut2KA

I was able to find out what the 'space' was by using the below code snippet:

var_dump(ord($vString{5}));
llanato
  • 2,508
  • 6
  • 37
  • 59
  • @derp, Thanks for the heads up, I'd prefer to use trim over preg_replace as trim has less of an overhead even though the overhead is very small in both cases. – llanato Aug 26 '14 at 21:56