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.
Formatted text to HTML table
Moderator: General Moderators
- 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
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.
Re: Formatted text to HTML table
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 ?
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 ?
- 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
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.
Re: Formatted text to HTML table
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
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