I have not had much luck with what I am trying to do with preg_replace. What I want to do is create a new BB Code (actually a few of them) for the Invision Power Board which does not have a BB Code for Tables. I have created the [table] [tr] [td] [th] and a [td c=] td r=] as well as the closing tags for all of them, but am having trouble with more than one attibute.
Here is the conversion I use for these tags that works:
$txt = preg_replace( "#\[table\](.+?)\[/table\]#is", "<TABLE>\\1</table>", $txt );
$txt = preg_replace( "#\[tr\](.+?)\[/tr\]#is", "<TR>\\1</TR>", $txt );
$txt = preg_replace( "#\[td\](.+?)\[/td\]#is", "<td>\\1</td>", $txt );
$txt = preg_replace( "#\[td r=(\S+?)\s*\](.+?)\[/td\]#is", "<td rowspan=\"\\1\">\\2</td>", $txt );
$txt = preg_replace( "#\[td c=(\S+?)\s*\](.+?)\[/td\]#is", "<td colspan=\"\\1\">\\2</td>", $txt );
and to convert back to BB Code when a user wishes to edit the message I have these conversions.
$txt = preg_replace( "#(<table>)(.+?)(</table>)#is","[table\]\\1\[/table\]", $txt );
$txt = preg_replace( "#(<tr>)(.+?)(</tr>)#is","[tr\]\\2\[/tr\]", $txt );
$txt = preg_replace( "#(<td>)(.+?)(</td>)#is","[td\]\\2\[/td\]", $txt );
$txt = preg_replace( "#(<th>)(.+?)(</th>)#is","[th\]\\2\[/th\]", $txt );
$txt = preg_replace( "#(<td colspan=\"(.+?)\"rowspan=\"\(.+?)\">)(.+?)(</td>)#is","[td c=\\1 r=\\2\]\\3\[/td\]", $txt );
$txt = preg_replace( "#(<td rowspan=\"(.+?)\">)(.+?)(</td>)#is","[td r=\\2\]\\3\[/td\]", $txt );
My problem is to convert the following which is gotten from the user with a JavaScript function:
[table bd=5 bdc=teal bgc=blue cp=3 cs=5]
and I want it to convert it to
<table border=5 bordercolor=teal bgcolor=blue cellpadding=3 cellspacing=5>
then reverse the process when the user or moderator or admin wants to edit the post.
I just can't seem to get the preg_replace to do right after the first attribut which is entered manually so the [td c=###] would be written and does not use a prompt script for values of border. How should I go about this or and I completely wrong with it?