Formatted text to HTML table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bgengine
Forum Newbie
Posts: 3
Joined: Thu Aug 19, 2010 9:32 am

Formatted text to HTML table

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Formatted text to HTML table

Post 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>";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
bgengine
Forum Newbie
Posts: 3
Joined: Thu Aug 19, 2010 9:32 am

Re: Formatted text to HTML table

Post 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 ?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Formatted text to HTML table

Post by AbraCadaver »

Just remove the array_filter() from the code I gave you.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
bgengine
Forum Newbie
Posts: 3
Joined: Thu Aug 19, 2010 9:32 am

Re: Formatted text to HTML table

Post 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
Post Reply