Page 1 of 1

Formatted text to HTML table

Posted: Thu Aug 19, 2010 9:50 am
by bgengine
HI, I've done a bit if php programming but am still pretty new..

Does anyone know of a script that can convert formatted text to a html table ?

I'm hoping to have a web site user paste in some text ( say a column from an excel sheet) into my page - I then convert it into a html table ( with borders etc ) and display it - then ask the user to confirm it.

There is a website that does this ( Google "Tableizer") .... I just need to understand how this is done so I can include it in my code.

Any help anyone could offer on this would be most greatful !

I'm hoping after the user confirms the data to write this to a database ... but thats for another day.

Re: Formatted text to HTML table

Posted: Thu Aug 19, 2010 3:56 pm
by AbraCadaver
Assuming that you have a form input named "text", then this is a quick example (just apply styles):

Code: Select all

echo "<table>\n";
foreach(array_filter(explode("\n", $_POST['text'])) as $row) {
	echo "<tr>\n";
	echo "<td>".implode("</td><td>", array_map('trim', explode("\t", $row)))."</td>\n";	
	echo "</tr>\n";
}
echo "</table>";

Re: Formatted text to HTML table

Posted: Fri Aug 20, 2010 3:12 am
by bgengine
Hi AbraCadaver,
Thanks, that's very close to what I'm looking for ....
It does what I'm looking for except in a case where I have a column of data and one entry is blank - is there any way I can keep the bank entry in the table I create ?

example: data entry is below ( 9 rows - all one column with a blank entry in row 4)

1234
5678
234

3456
6789
123

When I convert this to a table I get only 8 rows - and no blank row.
Alos - is it possible to process more than one column ?

Re: Formatted text to HTML table

Posted: Fri Aug 20, 2010 9:58 am
by AbraCadaver
Just remove the array_filter() from the code I gave you.

Re: Formatted text to HTML table

Posted: Fri Aug 20, 2010 1:06 pm
by bgengine
Thanks again for the reply ..

removing the array_filter() didn't seem to make a difference:

echo "<table>\n";
foreach(explode("\n", $_POST['details']) as $row) {
echo "<tr>\n";
echo "<td>".implode("</td><td>", array_map('trim', explode("\t", $row)))."</td>\n";
echo "</tr>\n";
}
echo "</table>";


I still get this

1
2
3
4
6
7
89

from this
1
2
3
4

6
7
89