Skip to content

Instantly share code, notes, and snippets.

@shouhei
Created November 30, 2015 06:13
Show Gist options
  • Select an option

  • Save shouhei/d43a0960ac9e36a81fbd to your computer and use it in GitHub Desktop.

Select an option

Save shouhei/d43a0960ac9e36a81fbd to your computer and use it in GitHub Desktop.
tsv形式のファイルを、HTMLのtableに出力する
<?php
if($argc != 2){
echo "引数が足りません".PHP_EOL;
exit;
}
$file = new SplFileObject($argv[1]);
echo '
<!DOCYTP html>
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-aUGj/X2zp5rLCbBxumKTCw2Z50WgIr1vs/PFN4praOTvYXWlVyh2UtNUU0KAUhAX" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha512-K1qjQ+NcF2TYO/eI3M6v8EiNYZfA95pQumfvcVrTHtwQVDG+aHRqLi/ETn2uB+1JqwYqVG3LIvdm9lj6imS/pQ==" crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
'.PHP_EOL;
echo '<table class="table table-bordered">'.PHP_EOL;
$row = $file->fgetcsv(' ');
$table_row = array_map(function($n){ return '<td>'.$n.'</td>'; }, $row);
$table_row_string = "<tr>".implode($table_row)."</tr>";
echo $table_row_string.PHP_EOL;
while(!$file->eof()){
$row = $file->fgetcsv(' ');
$table_row = array_map(function($n){ return '<td>'.$n.'</td>'; }, $row);
$table_row_string = "<tr>".implode($table_row, '')."</tr>".PHP_EOL;
echo $table_row_string.PHP_EOL;
}
echo "</table>
</div>
</body>
</html>".PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment