Created
September 29, 2012 09:22
-
-
Save jelmervdl/3803550 to your computer and use it in GitHub Desktop.
Derp to 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 | |
$input = trim(' | |
1. Max Havelaar 12.7 14.8 13.3 | |
2. Michiel [b]de[/b] Ruiter 14.4 11.8 13.3 14.4 | |
3. Awesome Stuff and others 15.568 11.3 12.1 | |
'); | |
function parse_table($input) | |
{ | |
$rows = array(); | |
foreach (explode("\n", $input) as $line) | |
$rows[] = preg_split("/\t+| {2,}/", trim($line)); | |
$n_of_columns = max(array_map('count', $rows)); | |
$html = "<table>\n"; | |
foreach ($rows as $row) | |
{ | |
$html .= "\t<tr>\n"; | |
for ($col = 0; $col < $n_of_columns; ++$col) | |
$html .= "\t\t<td>" . (isset($row[$col]) ? parse_ubb($row[$col]) : "") . "</td>\n"; | |
$html .= "\t</tr>\n"; | |
} | |
$html .= "</table>\n"; | |
return $html; | |
} | |
function parse_ubb($input) | |
{ | |
$input = preg_replace('/\[b\](.+?)\[\/b\]/', '<strong>$1</strong>', $input); | |
$input = preg_replace_callback('/\[table\](.+?)\[\/table\]/m', function ($matches) { | |
return parse_table($matches[1]); | |
}, $input); | |
return $input; | |
} | |
echo parse_table($input); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment