试图编写这样的函数。它必须将文本分成多个列,并且输出必须是有效的 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;
}
有效,但是当试图将一些文本分成 2 列时,我在第一列中得到未闭合的标签,在第二列中未打开。如何解决这个问题?需要帮忙!