2013-11-24 35 views
1

我试图让用户可以将表添加到他们的帖子,我遇到的问题,在另一个功能我使用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] 

See the extra space

+1

你能后的实际原始输出,而不是一个形象? – hwnd

+0

它在表格上方返回'
'标签,因为它不在一行(我提交的字符串,它使用'nl2br')。我添加了我正在使用的字符串。我用来测试的开发者网站就在这里:http://beta.projectdonut.me/docs/v/1385253660-documentation_Demo/updated如果有人想查看页面如何呈现它。 – Jake

+0

您是否想要移除'
'标记?还是全部空白? – hwnd

回答

0

添加其他功能,将您的文章的不同部分的护理。由于[table][\table]之间的部分明显与其他不同,你应该这样做:

function parsePost($post) 
{ 
    $table_start = mb_strpos($post, '[table]'); 
    $table_end = mb_strpos($post, '[/table]', $table_start); 
    $table_end += mb_strlen('[/table]'); 

    $output = nl2br(mb_substr($post, 0, $table_start)); 
    $output .= markupDoc(mb_substr($post, $table_start, $table_end - $table_start)); 
    $output .= nl2br(mb_substr($post, $table_end)); 
    return $output; 
} 

测试代码:http://ideone.com/sfRn33

+0

删除了一些额外的空间,但不是全部。 – Jake

+0

我忘了说:这甚至会支持多个表在一个字符串中? – Jake

+0

它没有删除哪个空格?不,它不会支持多个表,但不要指望我们为你编写代码:)你有配方​​,现在你可以按照它(提示:通过将上面的代码放在一个循环中)。 –

相关问题