Hello everybody,
I'd like to replace this...
|a|table|row|
|a|table|row|
...with this...
<table>
<tr><td>a</td><td>table</td><td>row</td></tr>
<tr><td>a</td><td>table</td><td>row</td></tr>
</table>
I'd use Textile but this is the only thing I need it for-- overkill.
Any ideas?
|a|simple|table| without Textile
Moderator: General Moderators
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Unlessthere's only ever two rows, with 3 columns it's more then just a single regex...
That's no regex at all 
Maybe there's a better way but that just came naturally
Code: Select all
$string = "|a|table|row|
|a|table|row|
";
function tabularize($input)
{
$ret = '<table>';
$rows = explode("\n", $input);
foreach ($rows as $col)
{
$cells = explode('|', $col)
$ret .= '<tr>';
foreach ($cells as $value)
{
$ret .= '<td>'.$value.'</td>';
}
$ret .= '</tr>';
}
$ret .= '</table>';
return $ret;
}Maybe there's a better way but that just came naturally