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