tags:

views:

36

answers:

4

I am reading in a flat text file, normally the first few lines are comments, how can I tell me script to ignore them?

The relevant section of the script is:

<?php
$delimiter = chr(1);
$eoldelimiter = chr(2) . "\n";
$fp = fopen('app2','r');
if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;}

$loop = 0;
while (!feof($fp)) {
  $loop++;
    $line = stream_get_line($fp,4096,$eoldelimiter); //use 2048 if very long lines
    $field[$loop] = explode ($delimiter, $line);
    echo '
<tr>
<td>'.$field[$loop][0].'</td>
<td>'.$field[$loop][1].'</td>
<td>'.$field[$loop][2].'</td>
<td>'.$field[$loop][3].'</td>
<td>'.$field[$loop][4].'</td>
<td>'.$field[$loop][5].'</td>
<td>'.$field[$loop][6].'</td>
<td>'.$field[$loop][7].'</td>
<td>'.$field[$loop][8].'</td>
<td>'.$field[$loop][9].'</td>
<td>'.$field[$loop][10].'</td>
<td>'.$field[$loop][11].'</td>
<td>'.$field[$loop][12].'</td>
<td>'.$field[$loop][13].'</td>
<td>'.$field[$loop][14].'</td>
<td>'.$field[$loop][15].'</td>
<td>'.$field[$loop][16].'</td>

</tr>';
    $fp++;
}

fclose($fp);
?>

The file looks like this:

#export_dateapplication_idtitlerecommended_ageartist_nameseller_namecompany_urlsupport_urlview_urlartwork_url_largeartwork_url_smallitunes_release_datecopyrightdescriptionversionitunes_versiondownload_size
#primaryKey:application_id
#dbTypes:BIGINTINTEGERVARCHAR(1000)VARCHAR(20)VARCHAR(1000)VARCHAR(1000)VARCHAR(1000)VARCHAR(1000)VARCHAR(1000)VARCHAR(1000)VARCHAR(1000)DATETIMEVARCHAR(4000)LONGTEXTVARCHAR(100)VARCHAR(100)BIGINT
#exportMode:FULL
1276678802857300728328Shanghai Pudong Miracle4+iJour Inc.Stanley Huanghttp://www.ijour.com.cn/http://www.ijour.com.cn/http://test.com/app/shanghai-pudong-miracle/id300728328?uo=5http://
A: 

I think you can do something with the explode php function. it will give you an array where you'll get '#' and other stuffs separated. Then with a recursive alorithm that would put only the elements that are not '#' in a new tab that should make it.

oleiade
explode() works with arrays. Line is a string. String functions are better.
dwich
+2  A: 
 function my_filter($var){
    return $var[0] != '#';
 }
 $array = array_filter(file('/path/to/file'),'my_filter');
Wrikken
+1  A: 

You probably want something like this between the $line=... and $field[$loop] = ... :

//Does not store this line if the first non-whitespace character is a #.
if( strpos(trim($line), '#') === 0  )
    continue;

The above also implies you move your $loop++ after the validation, unless you don't care about gaps in your array.

David L.-Pratte
+1  A: 

Like this:

while (!feof($fp)) {
    $line = stream_get_line($fp, 4096, $eoldelimiter); //use 2048 if very long lines

    if ($line[0] === '#') continue;  //Skip lines that start with #
    $loop++;
    ...
}
SLaks
thanks that has worked perfectly
kitenski