gos1 Posted December 13, 2006 Share Posted December 13, 2006 is there a way of reading only the first parameter in every line of a csv file. I looked at al lthe functions but did not find any answer for my problem. I mean I want my code to read every lines value until the first ';' Link to comment https://forums.phpfreaks.com/topic/30546-is-there-a-way/ Share on other sites More sharing options...
zq29 Posted December 13, 2006 Share Posted December 13, 2006 Could do it something like this...[code]<?php$h = fopen("filename.csv","r");while(($d = fgetcsv($h,1000,",")) !== FALSE) echo $d[0]."<br />";fclose($h);?>[/code] Link to comment https://forums.phpfreaks.com/topic/30546-is-there-a-way/#findComment-140644 Share on other sites More sharing options...
gos1 Posted December 14, 2006 Author Share Posted December 14, 2006 i tried that. Thats like the script in the php.net. But need to take only the first vcalue in every line. I can echo all the linea but cannot take the first variables in them thats my problem. Link to comment https://forums.phpfreaks.com/topic/30546-is-there-a-way/#findComment-140745 Share on other sites More sharing options...
hitman6003 Posted December 14, 2006 Share Posted December 14, 2006 [code]$file = file($filename);foreach ($file as $line) { $line = explode(",", $line); echo $line[0] . "<br />";}[/code] Link to comment https://forums.phpfreaks.com/topic/30546-is-there-a-way/#findComment-140746 Share on other sites More sharing options...
drifter Posted December 14, 2006 Share Posted December 14, 2006 well csv - comma seperated value - the code he gave was for comma - you asked for up to the first ;sounds like a delimiter problem Link to comment https://forums.phpfreaks.com/topic/30546-is-there-a-way/#findComment-140748 Share on other sites More sharing options...
doni49 Posted December 14, 2006 Share Posted December 14, 2006 The code hitman gave you will read each line one at a time and then break each line in to an array of items. Just look at the first item in that array. That's what echo $line[0]; does.Sample CSV File[quote]L1-Field1,L1-Field2,L1-Field3,L1-Field4,L1-Field5L2-Field1,L2-Field2,L2-Field3,L2-Field4,L2-Field5L3-Field1,L3-Field2,L3-Field3,L3-Field4,L3-Field5[/quote]If that is your CSV file, hitman's code will display[quote]L1-Field1L2-Field1L3-Field1[/quote] Link to comment https://forums.phpfreaks.com/topic/30546-is-there-a-way/#findComment-140843 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.