On 6/14/2011 10:35 AM, Ashley M. Kirchner wrote: > > I'm trying to extract data coming from an exec() call. The exec() call is > performing an ncdump on a netCDF file: > > <?php > $filename = "mlab.20110101.cdf"; > exec("/usr/bin/ncdump -l 2048 -v wmax ".$filename, $output); > echo "---\n"; > print_r($output); > echo "---\n"; > ?> > > This is all fine, however the data comes in with extra information I don't > need, and the data I do need is in a long string format: > > Array > ( > [0] => netcdf mlab.20110101 { > [1] => dimensions: > [2] => time = UNLIMITED ; // (288 currently) > [3] => variables: > [4] => int base_time ; > [5] => int samp_secs ; > [6] => float lat ; > [7] => float lon ; > [8] => float alt ; > [9] => int station ; > [10] => float time_offset(time) ; > [11] => float tdry(time) ; > [12] => float rh(time) ; > [13] => float pres(time) ; > [14] => float cpres0(time) ; > [15] => float dp(time) ; > [16] => float wdir(time) ; > [17] => float wspd(time) ; > [18] => float wmax(time) ; > [19] => float wsdev(time) ; > [20] => float wchill(time) ; > [21] => float raina(time) ; > [22] => float raina24(time) ; > [23] => float bat(time) ; > [24] => data: > [25] => > [26] => wmax = 5, 5, 5, 5.4, 5.7, 5.1, 7.1, 6.1, 4.4, 9.5, 13.5, 14.4, > 11.1, 9.6, 10.5, 10.6, 11.1, 10, 15.3, 17.2, 16.9, 16, 13.9, 16.7, 15.3, 18.3, > 17.2, 16.3, 15.9, 18.9, 17.7, 19.7, 19.7, 16.6, 16.4, 16, 14.6, 14.9, 14, 16.7, > 18.3, 16.2, 18.4, 15.4, 15.3, 13.3, 14.8, 15.6, 15.1, 14.7, 13.8, 14.2, 18, > 16.7, 16.6, 15.6, 15.8, 17.6, 17.4, 20.3, 17.4, 21.3, 18.7, 16.7, 15.4, 17.5, > 17.3, 17.7, 20.4, 17.5, 16.8, 18.1, 15.9, 17.8, 17.3, 13.9, 16.2, 17.4, 19.8, > 17.5, 19, 20, 20.3, 20.4, 20.8, 21.6, 26.4, 23.1, 21.3, 19.9, 20.1, 21.1, 22.1, > 22.5, 21.2, 20.2, 22.2, 21.6, 19.6, 21.1, 21.7, 21, 20.8, 23.7, 26.6, 21.2, > 23.8, 23.3, 23.5, 23.9, 21.4, 22.1, 23.5, 22.8, 23, 21.8, 22.2, 25.6, 21.9, > 22.8, 26.2, 24.2, 23.6, 25.5, 26.8, 25.6, 27.4, 24.5, 23.5, 22.1, 23.1, 25.5, > 22.5, 22.5, 23.8, 21.8, 22.6, 23.5, 24, 23, 22.3, 21.6, 23.6, 20.5, 21, 19.4, > 23.6, 19.2, 18, 19.2, 20.5, 19.3, 19.1, 20.4, 18, 17.8, 18.4, 19.4, 21, 18.3, > 20.4, 21.2, 22.1, 20.1, 17.3, 17.2, 17.6, 17.2, 19.9, 21, 20.6, 21.5, 23.8, 25, > 24, 23.9, 23.5, 27.7, 21.1, 20.8, 20.1, 18.6, 21, 18.7, 19.9, 16.4, 16.3, 17.3, > 11.8, 11.4, 15.2, 12.4, 14.2, 16.3, 21.6, 25, 19, 7.7, 9.7, 15.7, 7, 11.1, 10.3, > 11.2, 14.8, 11.7, 5.5, 5.5, 5.5, 6.9, 7.6, 6.5, 6.4, 6.7, 6.7, 4.9, 5.1, 4.7, > 5.7, 5.5, 9.2, 6.2, 7.1, 7.7, 6.5, 5.5, 6, 6.4, 7, 6.2, 6.7, 4.7, 3.8, 9.2, 8.1, > 7.6, 6.8, 6.3, 7.1, 10.5, 8, 9.9, 10.2, 12, 9.4, 14, 18, 11.9, 17.7, 20.9, 18.8, > 17.4, 13.5, 13.8, 12.6, 5.5, 6.1, 6.6, 6.3, 6.3, 5.1, 7.7, 5.8, 5.4, 4.9, 4.5, > 4.9, 4.7, 5, 4.8, 4.5, 5.6, 5.9, 4.6, 5.7, 7.2, 6.1, 6.4, 5.4, 6.1, 5.6, 5.9, > 6.4, 9.5, 11.2, 15.8, 15, 13.6 ; > [27] => } > ) > > ncdump doesn't provide an option to suppress the header information, and as > you can hopefully tell, the data I'm looking for is in one long string at key > position 26. > > How can I extract what I need from that (wmax) and have it as an array: wmax > = array(5, 5, ...) > > This is only one variable. I will be pulling more variables out, and they > all come out the same way as above, as strings. The headers might change, so I > can't simply say ignore the first 0-25 keys and expect what I need to be at > position 26. I need a reliable way of detecting where the data section starts. > And I need to convert the strings into arrays for each data set. > > Suggestions? > Here is a one liner that assumes a lot print_r(explode(', ', substr($output[count($output)-2)], 7)); Now, if you want a little more error checking and forgiveness, try this <?php ### all your stuff here ### $wmax = null; # Loop it all... foreach ( $output AS $line ) { # check to see if line starts with what I'm searching for... if ( strpos($line, 'wmax = ') === 0 ) { # Found something, strip beginning of string and explode rest $wmax = explode(', ', substr($line, 7)); # Since I found it, I don't need to continue; drop out break; } } # See if I have an array() if ( is_array($wmax) ) { # We have an array, now see if it is populated at all if ( $cnt = count($wmax) ) { # Great, tell them how many values we found echo 'I found '.$cnt.' wmax values and here they are.'.PHP_EOL; # Show them the values print_r($wmax); } else { # Darn, our array was empty, lets tell them the bad news... echo 'no wmax values were set'; } } else { # Dang, I couldn't find the wmax line, better let them know! echo 'could not identify the wmax values returned'; } ?> Jim Lucas -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php