csckid Posted November 25, 2009 Share Posted November 25, 2009 I have to insert the data from a text file that look like this: myname 12 23 myname2 42 myname3 14 34 myname4 21 How will I distinguish between 2nd and 4th row? thanks Link to comment https://forums.phpfreaks.com/topic/182895-string/ Share on other sites More sharing options...
trq Posted November 25, 2009 Share Posted November 25, 2009 How will I distinguish between 2nd and 4th row? Well, for starters, they are not at all the same. Link to comment https://forums.phpfreaks.com/topic/182895-string/#findComment-965353 Share on other sites More sharing options...
cags Posted November 25, 2009 Share Posted November 25, 2009 Assuming thats a tab delimited table, meaning there is a single tab between myname and 12 a double tab between 12 and 23 and a triple tab between myname2 and 42, then using explode on the tab character will allow you to access column 4 by using $output[3] etc. Link to comment https://forums.phpfreaks.com/topic/182895-string/#findComment-965357 Share on other sites More sharing options...
csckid Posted November 25, 2009 Author Share Posted November 25, 2009 if it is myname 12 23 myname2 42 myname335 14 34 myname4 21 then sigle tab won't work Link to comment https://forums.phpfreaks.com/topic/182895-string/#findComment-965363 Share on other sites More sharing options...
cags Posted November 25, 2009 Share Posted November 25, 2009 What? You mean if theres no tab between myname3 and 35? Of course it won't infact it's impossible for PHP to work out if that is myname3 and 35 or myname335 or myname33 and 5 without you giving it constraints. The whole point of a tab delimited table is it's delimited by tabs. The amount of tabs signifying which column is relevant. Link to comment https://forums.phpfreaks.com/topic/182895-string/#findComment-965367 Share on other sites More sharing options...
csckid Posted November 25, 2009 Author Share Posted November 25, 2009 I have to insert the data from a text file that look like this: myname 12 23 myname2 42 myname3 14 34 myname4 21 How will I distinguish between 2nd and 4th row? thanks When I read the file and print the index of each character; I expected that 3rd row should always start at index 48. But actually index is not the same. How do I fix this? this is how I am reading the txt file $file = fopen("mydata.txt", "r"); while (!feof($file)) { $data = fgets($file); for($i=0;$i<strlen($data);$i++){ echo $data[$i]." "."<font color='#CC6633'>".$i."</font>"; } } Link to comment https://forums.phpfreaks.com/topic/182895-string/#findComment-965432 Share on other sites More sharing options...
cags Posted November 25, 2009 Share Posted November 25, 2009 I just realised you said row not column. You can tell which row is which because independent rows are on different lines meaning they will be seperated by newline characters. Since fgets reads only an individual line I really don't understand what your trying to do. From your description though it really doesn't sound like you mean row. Perhaps if you gave an overal description of your objective we could help better. It looks like you wish to read in the file and treat it as a table in some manner. Ie retrieve information from each column. As with anything theres lots of ways of achieving that, but this seems a simple solution to what it sounds like your trying to do. $file = file_get_contents('file.txt'); $rows = explode("\r\n", $file); echo '<table>'; foreach($rows as $row) { echo '<tr>'; $columns = explode("\t", $row); foreach($columns as $column) { echo '<td>' . $column . '</td>'; } echo '</tr>'; } echo '</table>'; Link to comment https://forums.phpfreaks.com/topic/182895-string/#findComment-965438 Share on other sites More sharing options...
csckid Posted November 25, 2009 Author Share Posted November 25, 2009 Thanks..... There is one little problem Let me explain everything in detail what I wanted. I have to fetch data from a txt file that is on the internet and store the data in mysql. The original text file look like this: T5Y1212 100000 T5Y1213 100000 TAKAFULINS 451 TALLUSPIN 147.5 TBL 552.75 TITASGAS 695 TRUSTBANK 410 I found an sql function like: LOAD DATA INFILE 'mydata.txt' INTO TABLE txtfile FIELDS TERMINATED BY ';' So if I can prepare a txt file like this: T5Y1212; ; 100000 T5Y1213; ; 100000 TAKAFULINS; 451; TALLUSPIN; 147.5; TBL; 552.75; TITASGAS; ; 695 TRUSTBANK; 410; then I can easily load them in database. I tried your code and modified a bit echo '<td>' . $column . ';</td>'; The output was this: T5Y1212 ; ; 100000; T5Y1213 ; ; 100000; TAKAFULINS ; 451; TALLUSPIN ; 147.5; TBL ; ; 552.75; TITASGAS ; ; 695; TRUSTBANK ; 410; The error was in this line TBL ; ; 552.75; it was suppose to print like this: TBL ; 552.75; Thats all Link to comment https://forums.phpfreaks.com/topic/182895-string/#findComment-965545 Share on other sites More sharing options...
cags Posted November 25, 2009 Share Posted November 25, 2009 That being the case, the obvious choice would have been to use MySQL to load up the current format you have. Ie... LOAD DATA INFILE 'mydata.txt' INTO TABLE txtfile FIELDS TERMINATED BY '\t' But then I've never used LOAD DATA INFILE. Plus you should get the same issue since there are obviously multiple tabs in the input. Link to comment https://forums.phpfreaks.com/topic/182895-string/#findComment-965547 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.