Ignoring lines that begin with #
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_nam开发者_JAVA百科ecompany_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://
function my_filter($var){
return $var[0] != '#';
}
$array = array_filter(file('/path/to/file'),'my_filter');
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++;
...
}
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.
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.
精彩评论