Page 1 of 1

|a|simple|table| without Textile

Posted: Wed Oct 26, 2005 5:38 pm
by tom_jones
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?

Posted: Wed Oct 26, 2005 5:56 pm
by Chris Corbyn
Unlessthere's only ever two rows, with 3 columns it's more then just a single regex...

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;
}
That's no regex at all :P

Maybe there's a better way but that just came naturally :)

Posted: Thu Oct 27, 2005 12:51 pm
by tom_jones
Wow, that was fast-- Thanks, d11wtq!