I am creating a content management system (PHP/MySQL) and want to be
able to create HTML tables in an easy way. I want the user to be able
to insert comma deliniated data between a "<table>" tag and to have
the HTML created by a PHP script.
For example, if the user enters:
<table>
A,B,C,D
1,2,3,4
a,b,c,d
i,ii,iii,iv
</table>
I want the script to create:
<table>
<tr><td>A</td><td>B</td><td>C</td><td>D</td></tr>
<tr><td>1</td><td>2</td><td>3</td><td>4</td></tr>
<tr><td>a</td><td>b</td><td>c</td><td>d</td></tr>
<tr><td>i</td><td>ii</td><td>iii</td><td>iv</td></tr>
</table>
I am guessing that I need a preg_replace script that will replace
commas with "</td><td>" and carriage returns with
"</td></tr><tr><td>".
I would be keen for someone to write a working script for me, as the
preg_replace code is a complete mystery to me. |
Request for Question Clarification by
hammer-ga
on
11 Mar 2004 18:36 PST
Can we depend on only one table construct being handed to the script,
or might the user type in more than one at a time? Like so:
<table>
A,B,C,D
1,2,3,4
</table>
<table>
a,b,c,d
i,ii,iii,iv
</table>
- Hammer
|
Clarification of Question by
shabbataizevi-ga
on
12 Mar 2004 14:50 PST
The script currently gets the text that will be displayed on a webpage
out of a database.
The php script then does some formatting (such as making text between
square brakcets bold). The code that I am wanting, to create tables
out of tabulated text, will go in the code below.
Note that that there may not be any tables in the text string, or that
may be one or two or more tables.
Also, there will obviously be lots of commas and carriage returns in
the rest of the text, and so only commas and carriage returns between
the "<table>" and "</table>" tags should be affected.
Let me know if you need any further clarification.
This is the code as is stands at the moment:
================================================
$txt = $myrow["txt"];
$txt = str_replace("->", "<Blockquote>", $txt);
$txt = str_replace("<-", "</Blockquote>", $txt);
$txt = str_replace("[", "<B>", $txt);
$txt = str_replace("]", "</B>", $txt);
$txt = preg_replace("/\b((http(s?):\/\/)|(www\.))([\w\.-]+)([\/\w+\.-]+)\b/i",
"<a href=\"http$3://$4$5$6\" target=\"_blank\">$2$4$5$6</a>", $txt);
$txt = preg_replace("/[\w\.-]+(@)[\w\.-]+/i", "<a
href=\"mailto:$0\">$0</a>", $txt);
YOUR CODE WILL GO HERE!!
echo $txt;
================================================
|
Request for Question Clarification by
sycophant-ga
on
14 Mar 2004 02:03 PST
Hi,
I have made the following code, pretty quickly.
However it can't match more than one <table>...</table> in a given
string. Changing the 'if' for a 'while' makes it able to, but also
induces an infinate loop.
I have use str_replace, as the PHP documention suggests it is faster
than preg_replace for simple replace operations.
It shouldn't be hard to make it work right, but I have run out of time
at the moment. If it's still unanswered when I am next free, I will
make it work, but in the meantime I hope this helps you get the answer
you need.
$reg = "<table>(.*)<\/table>";
if (ereg($reg,$txt,$match)) {
$replace = "<table><tr><td>".$match[1]."</td></tr></table>";
$replace = str_replace("\n","</td></tr>\n<tr><td>",$replace);
$replace = str_replace(",","</td><td>",$replace);
$txt = str_replace($match[0],$replace,$txt);
}
Regards,
Sycophant-ga
|