PHP为此提供了一个函数,称为'str_split()'。你可以像这样使用它:
<?php
$content = get_the_content();
$chunks = str_split($content, 500);
//Printing each chunk in a div
foreach($chunks as $chunk_content) {
echo "<div>";
echo $chunk_content;
echo "</div>";
}
?>
str_split 的更多信息:http ://www.php.net/manual/en/function.str-split.php
编辑:如果单词不应该在中间被剪切,请改用这个函数:
<?php
$content = get_the_content();
$strings = wordwrap($content, 500, "{BREAK}"); //Put a {BREAK} every 500 characters
$chunks = explode("{BREAK}", $strings); //Put each segment separated by {BREAK} into an array field
//Printing each chunk in a div
foreach($chunks as $chunk_content) {
echo "<div>";
echo $chunk_content;
echo "</div>";
}
?>
如果你想节省一些内存,你可以像这样组合这些函数:
<?php
foreach(explode("{BREAK}", wordwrap(get_the_content(), 500, "{BREAK}")) as $chunk) {
echo "<div>" . $chunk . "</div>\n"; //The \n creates a new line
}
?>
有关自动换行的更多信息,请参见http://www.php.net/manual/en/function.wordwrap.php