x_maras Posted November 25, 2009 Share Posted November 25, 2009 Hi, I want to read a file which is of unknown format. It has experimental data in rows and columns. The first like has the names of each column and then in each line are some numbers. Between every word or number result there are spaces, but in some there is one space in other 2-3 and in some even 4-5. an example of how the results are in the file is 167160 99999 19960101 57.0 24 52.7 24 1002.5 24 1000.7 24 6.0 24 10.1 24 22.0 30.9 61.5 53.6* 167160 99999 19960102 57.3 23 49.5 23 1003.6 23 1001.8 23 7.9 23 3.3 23 7.0 999.9 62.6 51.8* 167160 99999 19960103 54.9 24 45.7 24 1002.6 24 1000.8 24 7.6 24 8.4 24 15.0 999.9 57.9 51.8* So I dont know how i would be able to read this file and work with the columns I want to. I would appreciate some help in this. Quote Link to comment https://forums.phpfreaks.com/topic/182961-reading-a-file-with-a-lot-of-spaces-between-words/ Share on other sites More sharing options...
Alex Posted November 25, 2009 Share Posted November 25, 2009 You can do this using preg_split. In the code I put some examples so you can see how you would access the data from the $records array. $file = <<<FILE 67160 99999 19960101 57.0 24 52.7 24 1002.5 24 1000.7 24 6.0 24 10.1 24 22.0 30.9 61.5 53.6* 167160 99999 19960102 57.3 23 49.5 23 1003.6 23 1001.8 23 7.9 23 3.3 23 7.0 999.9 62.6 51.8* 167160 99999 19960103 54.9 24 45.7 24 1002.6 24 1000.8 24 7.6 24 8.4 24 15.0 999.9 57.9 51.8* FILE; //$file = file_get_contents('somefile.txt'); $rows = explode("\n", $file); foreach($rows as $row) $records[] = preg_split('~\s+~', $row); echo $records[0][0]; // 67160 echo $records[0][2]; // 19960101 echo $records[1][1]; // 99999 Quote Link to comment https://forums.phpfreaks.com/topic/182961-reading-a-file-with-a-lot-of-spaces-between-words/#findComment-965715 Share on other sites More sharing options...
x_maras Posted November 26, 2009 Author Share Posted November 26, 2009 Worked perfectly! Thousand thank you! Can I also ask something? The parameter in '~\s+~', what does exactly? Quote Link to comment https://forums.phpfreaks.com/topic/182961-reading-a-file-with-a-lot-of-spaces-between-words/#findComment-965767 Share on other sites More sharing options...
cags Posted November 26, 2009 Share Posted November 26, 2009 The \s means whitespace character and the + means 1 or more. The tildes (~)'s are just delimiters, which are required with PCRE regular expressions. On a side note, the input you provided initially looks suspiciously like a tab delimited file. If it is then you could just use... $rows = explode("\n", $file); foreach($rows as $row) { $columns = explode("\t", $row); foreach($columns as $column) { } } Quote Link to comment https://forums.phpfreaks.com/topic/182961-reading-a-file-with-a-lot-of-spaces-between-words/#findComment-965948 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.