Skip to content

Instantly share code, notes, and snippets.

@trepidity
Created September 10, 2020 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trepidity/70ab2137fa2fb887eb3f9470647ed4f1 to your computer and use it in GitHub Desktop.
Save trepidity/70ab2137fa2fb887eb3f9470647ed4f1 to your computer and use it in GitHub Desktop.
<?php
$delimiter=",";
$src_file = $argv[1];
$tmpl_file = $argv[2];
if (file_exists($src_file)) {
if (file_exists($tmpl_file)) {
$src_handle = fopen($src_file,"r");
$tmpl_handle = fopen($tmpl_file,"r");
$src_row = 1;
while (!feof($src_handle)) {
$src_buffer = fgetcsv($src_handle, 4096, $delimiter);
$src_num = count($src_buffer);
if ($src_row == 1) {
for ($src_c=0; $src_c < $src_num; $src_c++) {
$src_headers = $src_buffer;
}
} else {
if (@strlen($src_buffer)) {
while (!feof($tmpl_handle)) {
$tmpl_buffer = fgets($tmpl_handle, 4096);
for ($src_c=0; $src_c < $src_num; $src_c++) {
$tmpl_buffer = preg_replace("/%".$src_headers[$src_c]."%/",$src_buffer[$src_c],$tmpl_buffer);
}
echo $tmpl_buffer;
}
}
}
$src_row++;
}
$tmpl_close = fclose($tmpl_handle);
$src_close = fclose($src_handle);
} else {
die ("\r\nTemplate file does not exist!\r\nUsage: php convert.php [source_file] [template_file] > [target_file]\r\n\r\n");
}
} else {
die ("\r\nSource file does not exist!\r\nUsage: php convert.php [source_file] [template_file] > [target_file]\r\n\r\n");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment