2013-11-24 34 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

它沒有刪除哪個空格?不,它不會支持多個表,但不要指望我們爲你編寫代碼:)你有配方​​,現在你可以按照它(提示:通過將上面的代碼放在一個循環中)。 –

相關問題