Forum Moderators: coopster

Message Too Old, No Replies

fgetcsv() with a .csv TAB Delimited File Spits out Special Characters?

         

kazisdaman3

8:40 am on Mar 7, 2008 (gmt 0)

10+ Year Member



I'm trying to upload a TAB Delimited .csv to PHP, and it spews out special characters between each word. It seems to work fine when uploading a real, "comma" delimited .csv file, but with "tab" delimited it spews out all theses unreadable characters between each word?

--

$tmp_name = $_FILES['csv']['tmp_name'];
$handle = fopen($tmp_name, "r");

while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) { print_r($data); }

--

prints out something like this when using a tab delimited .csv file

D�e�s�c�r�i�p�t�i�o�n� �L�i�n�e� �1�
D�e�s�c�r�i�p�t�i�o�n� �L�i�n�e� �2�
D�i�s�p�l�a�y� �U�R�L�
D�e�s�t�i�n�a�t�i�o�n� �U�R�L�
C�a�m�p�a�i�g�n� �S�t�a�t�u�s�

--

when using commas for the delimits, in a .csv file it prints out normal like this:

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { print_r($data); }

Description Line 1
Description Line 2
Display URL
Destination URL
Campaign Status

---

Why is is messing up so much when I insert a tab delimited CSV, I can't seem to figure it out for the life of me, and I've searched all over!

kazisdaman3

10:49 am on Mar 7, 2008 (gmt 0)

10+ Year Member



looks like it doesn't even have anything to do with fgetcsv(), when I even echo out a fgets() of the file, it still returns those crazy characters when it is a tab delimited file? Any suggestions?

jatar_k

1:32 pm on Mar 7, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



looks like the chars are actually in the file then, why not just strip them?

coopster

9:37 pm on Mar 7, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Force binary mode when you open the file (details on the manual page for fopen [php.net]):
$handle = fopen($tmp_name, "rb");

kazisdaman3

3:34 am on Mar 8, 2008 (gmt 0)

10+ Year Member



Strange it kept doing it, I finally had to run a function that would clear out EVERYTHING BUT real characters, it was must been inserting some crazy symbols or something and screwing everything up: the function:

cleanString($text); returns cleared out string:

function cleanString( $string = "" ) {

$allowedCharacters= array( " ", "a", "b", "c", "d", "e", "g", "h", "i", "j",
"k", "l", "m", "n", "o", "p", "q", "r", "s",
"t", "u", "v", "w", "x", "y", "z",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
"‘", "’", "‚", "“",
"”",
"„",
"†",
"‡",
"‰",
"‹",
"›",
"♠",
"♣",
"♥",
"♦",
"★",
"☆",
"‾",
"←",
"↑",
"→",
"↓",
"™",

"!",
"\"",
"#",
"$",
"%",
"&",
"'",
"(",
")",
"*",
"+",
",",
"-",
".",

"/",

":",
";",
"<",
"=",
">",
"?",
"@",

"[",
"\\",
"]",
"^",
"_",
"`",

"{",
"¦",

"}",
"~",

"–",
"—",

"¡",
"¢",
"£",
"¤",
"¥",
"¦",
"§",
"¨",
"©",
"ª",
"«",
"¬",
"­",
"®",
"¯",

"°",
"±",
"²",
"³",
"´",
"µ",
"¶",
"·",
"¸",
"¹",
"º",
"»",
"¼",
"½",
"¾",
"¿",
"À",
"Á",
"Â",

"Ã",
"Ä",
"Å",
"Æ",
"Ç",
"È",
"É",
"Ê",
"Ë",
"Ì",
"Í",
"Î",
"Ï",
"Ð",
"Ñ",
"Ò",
"Ó",
"Ô",
"Õ",

"Ö",
"×",
"Ø",
"Ù",
"Ú",
"Û",
"Ü",
"Ý",
"Þ",
"ß",
"à",
"á",
"â",
"ã",
"ä",
"å",
"æ",
"ç",
"è",

"é",
"ê",
"ë",
"ì",
"í",
"î",
"ï",
"ð",
"ñ",
"ò",
"ó",
"ô",
"õ",
"ö",
"÷",
"ø",
"ù",
"ú",
"û",

"ü",
"ý",
"þ",
"ÿ" );
$newString .= "";

for( $incString = 0; $incString < strlen( $string ); $incString++ ) {

if( in_array( strtolower( $string[$incString] ), $allowedCharacters ) ) {

$newString .= $string[$incString];
}
}
return $newString;
}