【发布时间】:2013-12-08 19:20:49
【问题描述】:
我正在尝试这样做,以便用户可以在他们的帖子中添加一个表格,这是我遇到的问题,在另一个函数中,我使用nl2br 来制作它,以便它自动添加换行符。虽然,表格是如何工作的,但所有换行符都会超出它,从而导致大量空间。我在下面提供了一张图片,以便您了解我的意思。我尝试使用\r\n 或\n,但我觉得我要么用错了,要么在这种情况下不起作用。
function markupDoc($post){
//tables
while(($post2 = preg_replace("/\[td]((?:(?!\[td\]).)+?)\[\/td\]/s", '<td>\\1</td>', $post)) !== $post){
$post=$post2;
}
while(($post2 = preg_replace("/\[tr]((?:(?!\[tr\]).)+?)\[\/tr\]/s", "<tr>\\1</tr>", $post)) !== $post){
$post=$post2;
}
while(($post2 = preg_replace("/\[thead]((?:(?!\[thead\]).)+?)\[\/thead\]/s", '<thead>\\1</thead>', $post)) !== $post){
$post=$post2;
}
while(($post2 = preg_replace("/\[table]((?:(?!\[table\]).)+?)\[\/table\]/s", '<table class="table table-striped table-bordered">\\1</table>', $post)) !== $post){
$post=$post2;
}
return $post;
}
我要提交的字符串:
going to
[table]
[thead]
[td]test[/td]
[td]test[/td]
[td]moo[/td]
[td]a[/td]
[/thead]
[tr]
[td]test[/td]
[td]moo[/td]
[/tr]
[tr]
[td]test[/td]
[td]test[/td]
[td]moo[/td]
[td]a[/td]
[/tr]
[/table]
【问题讨论】:
-
你能发布实际的原始输出而不是图像吗?
-
它在表格上方返回
<br>标签,因为它不在一行上(我提交的字符串,它使用nl2br)。我添加了我正在使用的字符串。我用来测试的开发者网站就在这里:beta.projectdonut.me/docs/v/1385253660-documentation_Demo/… 如果有人想查看页面如何呈现它。 -
您要删除
<br />标签吗?还是所有的空白? -
在
[table]之间,我希望将其删除。您可以看到它是如何在呈现的表格上方添加<br>标记的,因为这就是表格的工作方式,而且我的字符串是多行的。 -
它在表格的每一行之后添加
<br />标签。
标签: php regex preg-replace bbcode