Skip to content

Instantly share code, notes, and snippets.

@ekka21
Created August 16, 2012 17:25
Show Gist options
  • Save ekka21/3371834 to your computer and use it in GitHub Desktop.
Save ekka21/3371834 to your computer and use it in GitHub Desktop.
php: convert strings to table fixed columns
<?php
/**
* @Desc: Convert str1,str2,str3,str4,str5,....... to fix table columns
* @param string, delimiter, number of column
* @return Table markup fix columns
*/
function pre_table($str, $del = ',', $numCols = '3'){
$cnt = 0;
$arr = explode($del,$str);
$out_str = "<table width='100%'>";
$cnt=0;
for ($cnt=0;$cnt<count($arr); $cnt++)
{
if ($cnt % $numCols == 0)
{
if ($cnt > 0) $out_str .= "</tr>";
$out_str .= "<tr valign='top'>";
}
$out_str .= "<td>".$arr[$cnt]."</td>";
}
while ($cnt % $numCols != 0)
{
$out_str .= "<td>&nbsp;</td>";
$cnt++;
}
$out_str .= "</tr></table>";
return $out_str;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment