Last active
December 15, 2015 18:30
-
-
Save nidorx/5304661 to your computer and use it in GitHub Desktop.
Cria tabelas HTML a partir de formatação em arquivo texto puro. As linhas iniciadas por "|!" são os itens opcionais da tabela (colgroup, caption, classe e etc). O resultado é a tabela html formatada "<table></table>".
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Cria tabelas HTML a partir de formatação em arquivo texto puro | |
| * | |
| * | |
| * <pre><code> | |
| * | Header 1 | Header 2 | |
| * |! | class="table1" | |
| * |!caption | Caption da tabela 2 | |
| * |!colsgroup | style="background-color:red" | class="span1" | |
| * |--|-- | |
| * |Cell 1 | Cell 2 | |
| * |Cell 3 | Cell 4 | |
| * </code></pre> | |
| * | |
| * Exemplo: | |
| * <pre></code> | |
| * <?php | |
| * $text = ' | |
| * | |
| * Texto puro ... | |
| * | |
| * | Header 1 | Header 1 | |
| * |! | class="table1" | |
| * |!caption | Caption da tabela 1 | |
| * |!colgroup | style="background-color:red" | class="span1" | |
| * |--|-- | |
| * |Cell 1 | Cell 2 | |
| * |Cell 3 | Cell 4 | |
| * | |
| * Continuação do texto puro .... | |
| * | |
| * | Header 1 | Header 2 | |
| * |! | class="table2" | |
| * |!caption | Caption da tabela 2 | |
| * |--|-- | |
| * |Cell 1 | Cell 2 | |
| * |Cell 3 | Cell 4 | |
| * | |
| * Continuação do texto puro ...'; | |
| * | |
| * echo tableFromString($text); | |
| * </code></pre> | |
| * | |
| * @param string $text | |
| * @return string Tabela html formatada | |
| */ | |
| function tableFromString($text) | |
| { | |
| $callback = function($matches) { | |
| $html = ''; | |
| $htmlTableParams = ''; | |
| $htmlTableCaption = ''; | |
| $htmlTableColgroup = ''; | |
| $htmlTableThead = ''; | |
| //Criando o head da tabela | |
| $headerTxt = trim(preg_replace('/[|] *$/m', '', $matches['header'])); | |
| $header = explode('|', $headerTxt); | |
| $col_count = count($header); | |
| $htmlTableThead = '<thead><tr>'; | |
| foreach ($header as $th) { | |
| $htmlTableThead .= "<th>$th</th>"; | |
| } | |
| $htmlTableThead .= '</tr></thead>'; | |
| //Tratando os parametros opcionais da tabela | |
| $params = explode("\n", $matches['params']); | |
| foreach ($params as $param) { | |
| if (preg_match('/^ *\|\!(?<name>(\w+)?).*?\|(?<data>.*)/', $param, $matchesParam)) { | |
| switch ($matchesParam['name']) { | |
| case ''://Parametros da tabela | |
| $htmlTableParams = ' ' . trim($matchesParam['data']); | |
| break; | |
| case 'caption'://Caption da tabela | |
| $htmlTableCaption = '<caption>' . trim($matchesParam['data']) . '</caption>'; | |
| break; | |
| case 'colgroup'://Colsgroup da tabela | |
| $colgroup = explode('|', $matchesParam['data']); | |
| $htmlTableColgroup = ''; | |
| if (!empty($colgroup)) { | |
| $htmlTableColgroup .= '<colgroup>'; | |
| foreach ($colgroup as $groupParams) { | |
| $p = trim($groupParams); | |
| $htmlTableColgroup .= "<col {$p}>"; | |
| } | |
| $htmlTableColgroup .= '</colgroup>'; | |
| } | |
| break; | |
| default: | |
| break; | |
| } | |
| } | |
| } | |
| //Criando o body da tabela | |
| $contentTxt = preg_replace('/^ *[|]/m', '', $matches['cells']); | |
| $rows = explode("\n", trim($contentTxt, "\n")); | |
| $htmlBody = '<tbody>'; | |
| foreach ($rows as $row) { | |
| $htmlTr = '<tr>'; | |
| $row_cells = array_pad(preg_split('/ *[|] */', $row, $col_count), $col_count, ''); | |
| foreach ($row_cells as $cell) { | |
| $htmlTr.="<td>$cell</td>"; | |
| } | |
| $htmlTr .= '</tr>'; | |
| $htmlBody .= $htmlTr; | |
| } | |
| $htmlBody .= '<tbody>'; | |
| //Finalizando a tabela | |
| $html = "<table{$htmlTableParams}>{$htmlTableColgroup} {$htmlTableCaption} {$htmlTableThead} {$htmlBody}</table>"; | |
| return $html; | |
| }; | |
| return preg_replace_callback('/^ # Inicio da linha | |
| \|(?<header>(.+))\n # $1: Cabeçalhos | |
| (?<params>( # $3: params | |
| (\|\!.* \n)* # Parametros | |
| )) | |
| \|[-]+(.*)? \n # Separador | |
| (?<cells>( # $4: Células | |
| ([ ]*\|.* \n)* # Conteudo da row | |
| )) | |
| /xm', $callback, $text); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment