BBcode表(PHP)导致的空间过多

BBcode表(PHP)导致的空间过多,php,html-table,str-replace,bbcode,nl2br,Php,Html Table,Str Replace,Bbcode,Nl2br,我已经在谷歌上搜索过很多次了,但我就是想不出来。希望你能帮助我 我正在为留言板编程。系统从回复中过滤HTML,并强制我的成员使用标准BBcode。所有的基本BBCODE都运行顺利,但是我的表代码遇到了障碍 BBcode 这是控制我留言板上BBcode的脚本。这是非常简单和直接的。我只关心[table][tr]和[td],但我加入了[b],以便您了解该函数处理的不仅仅是我正在使用的三个代码 function BBcode($original_string) { $find = array( '/

我已经在谷歌上搜索过很多次了,但我就是想不出来。希望你能帮助我

我正在为留言板编程。系统从回复中过滤HTML,并强制我的成员使用标准BBcode。所有的基本BBCODE都运行顺利,但是我的表代码遇到了障碍

BBcode

这是控制我留言板上BBcode的脚本。这是非常简单和直接的。我只关心[table][tr]和[td],但我加入了[b],以便您了解该函数处理的不仅仅是我正在使用的三个代码

function BBcode($original_string) {

$find = array(
'/\[b\](.*?)\[\/b\]/is',
'/\[table\](.*?)\[\/table\]/is',
'/\[tr\](.*?)\[\/tr\]/is',
'/\[td\](.*?)\[\/td\]/is'
);

$replace = array(
'<b>$1</b>',
'<table border="1" cellpadding="5">$1</table>',
'<tr>$1</tr>',
'<td>$1</td>
);

$new_string = preg_replace ($find, $replace, $original_string);

return $new_string;

}
太棒了!这是按计划进行的

问题

尽管它有一个主要缺陷——如果一个成员使用我上面提供的BBcode提交表,nl2br会添加一大堆无用的空格。你可以明白我在这里的意思:

想象一下,每增加一个TR,情况会变得多么糟糕

但是,如果用户像这样发布评论:

[table] [tr] [td]Cell 1[/td] [td]Cell 2[/td] [td]Cell 3[/td] [/tr] [tr] [td]x[/td] [td]y[/td] [td]z[/td] [/tr] [/table]
他们发表的评论看起来不错:

我已经试过了

我发现这是因为我在这些评论中使用了nl2br。目前,它在BBcode函数运行之前添加BR标记。我试着在BBcode之后移动nl2br,但没有成功

接下来,我回到BBcode函数并尝试在数组中进行str_替换

function BBcode($original_string) {

$find = array(
'/\[b\](.*?)\[\/b\]/is',
'/\[table\](.*?)\[\/table\]/is',
'/\[tr\](.*?)\[\/tr\]/is',
'/\[td\](.*?)\[\/td\]/is'
);

$replace = array(
'<b>$1</b>',
'<table border="1" cellpadding="5">' .print str_replace("<br />", "", "$1"). '</table>',
'<tr>$1</tr>',
'<td>$1</td>
);

$new_string = preg_replace ($find, $replace, $original_string);

return $new_string;

}
我还尝试了无数其他方法来写这句话,例如:

'<table border="1" cellpadding="5">' .str_replace("<br />", "", "$1"). '</table>',
'<table border="1" cellpadding="5">' .str_replace("<br />", "", "$1"). '$1</table>',
'<table border="1" cellpadding="5">' .str_replace("<br />", "", "$1"). '$1</table>',
'<table border="1" cellpadding="5">' . $thing = str_replace("<br />", "", "$1"); . '$thing</table>',
'<table border="1" cellpadding="5">' . $thing = str_replace("<br />", "", "$1") . '$thing</table>',
'<table border="1" cellpadding="5">' . print str_replace("<br />", "", $1); . '</table>',
etc
基本上,我想做的是替换[table]代码中的所有BR标记。我该怎么做?我做错了什么?我应该改变我的方法吗

如果这个问题在别处得到了回答,我提前表示歉意。我在其他论坛上看到至少有三个人有同样的问题,但老年退休金计划都链接到了同一个地方——一个在过期链接上给出解决方案的线程。但是如果我错过了什么,请联系我


如果你有任何问题,请提问

我遇到这个问题时所做的是隐藏br/

我把桌子放在一个定制的div里

'<table border="1" cellpadding="5">' .str_replace("<br />", "", "$1"). '</table>',
'<table border="1" cellpadding="5">' .str_replace("<br />", "", "$1"). '$1</table>',
'<table border="1" cellpadding="5">' .str_replace("<br />", "", "$1"). '$1</table>',
'<table border="1" cellpadding="5">' . $thing = str_replace("<br />", "", "$1"); . '$thing</table>',
'<table border="1" cellpadding="5">' . $thing = str_replace("<br />", "", "$1") . '$thing</table>',
'<table border="1" cellpadding="5">' . print str_replace("<br />", "", $1); . '</table>',
etc
$replace = array(
    '<div class="tablepost"><table border="1" cellpadding="5">' .print str_replace("<br />", "", "$1"). '</table></div>',
.tablepost > br {
  content: ' '
}