3
試圖編寫此類函數。它必須將文本分成多個列,並且輸出必須是有效的html,例如沒有未打開的(!!!)關閉標籤,沒有未封閉的標籤。這裏是我的代碼:將字符串拆分爲多列(標籤可識別)
function convert2columns($content = '', $columns = 2) {
$result = array();
$content = closetags($content);
$bodytext = array("$content");
$text = implode(",", $bodytext);
$length = strlen($text);
$length = ceil($length/$columns);
$words = explode(" ", $text);
$c = count($words);
$l = 0;
for ($i = 1; $i <= $columns; $i++) {
$new_string = "";
for ($g = $l; $g <= $c; $g++) {
if (strlen($new_string) <= $length || $i == $columns) {
if (in_array(substr(@$words[$g], $length - 1, 1), array(' ', '.', '!', '?')))
$new_string .= @$words[$g] . " ";
else {
$split = substr(@$words[$g], 0, $length - 1);
$lastSpace = strrpos($split, ' ');
if ($lastSpace !== false) {
$split = substr($split, 0, $lastSpace);
}
if (in_array(substr($split, -1, 1), array(','))) {
$split = substr($split, 0, -1);
}
$new_string .= $split . " ";
}
} else {
$l = $g;
break;
}
}
$result[] = $new_string;
}
return $result;
}
的作品,但是當試圖某些文本分成兩列,我得到了第一列和第二未拆封未關閉的標籤。如何解決這個問題?需要幫忙!
任何時候你使用字符串函數(或正則表達式)來做_anything_與HTML,你一定會遇到問題。也就是說,你得到的和你想要的東西的一些樣本輸出將會有所幫助。 :) – Herbert
closetags()做什麼?你能告訴我們這個方法嗎? – Chris
嗨Zhlobopotam。我剛開始研究同樣的問題,並樂於分享我的最終解決方案,如果它有幫助。正如@Chris所說 - closetags()做了什麼? – BaronGrivet