2012-01-10 19 views
-3

那麼,假設我有一段文本可以是1000行或更多,並且我希望每200行剪切一個塊並將其包裝在一個div中?使用php剪切文本塊幷包裝它

我可以找到一個解決方案,瑪比你們可以給我一個片段,如果你喜歡開始。

謝謝。

+2

我們是在說線條還是說話?行不是固定的長度... – nico 2012-01-10 22:42:58

+0

@ nico words sir – Uffo 2012-01-10 22:46:30

回答

1

假設你的線條與\n分離:

// Split text into separate lines 
$lines = explode("\n",$text); 

// This will hold the resulting string 
$output = ''; 

// Loop the array 200 lines at a time 
for ($pos = 0, $linesLeft = count($lines); $linesLeft > 200; $pos += 200, $linesLeft -= 200) { 
    $output .= '<div>'.implode("\n",array_slice($lines,$pos,200))."</div>\n"; 
} 

// Add the last block, if any 
if ($linesLeft > 0) { 
    $output .= '<div>'.implode("\n",array_slice($lines,$pos))."</div>"; 
} 

編輯如果你正在處理的話,而不是線,只用空格代替\n。或者做:

$lines = preg_split('/\s+/',$text); 
+0

OMG謝謝sooooo你的時間,正確的解決方案是使用'$ lines = preg_split('/ \ s + /',$ text);'thank youuu – Uffo 2012-01-10 23:00:07