我正在创建一个应用程序,用户应该尽可能正确地键入文本。文本来自数据库并放置在字符串中。我在 div
中显示字符串,如果字符串的长度对于 div
来说太长,我想将其拆分。我知道以前也有过类似的问题,但我还没有找到解决我的问题的方法。
这就是我想做的:
Atm 发生这种情况:
--------------------
| The displayed str|ing
--------------------
我想要这个:
--------------------
| The displayed st |
--------------------
这是我到目前为止的代码:
while ($row = mysqli_fetch_array($result)) {
$textline = explode(".", $row["text"]);
}
$i = 0;
foreach ($textline as $o) {
$letters = str_split_unicode($letters[$i], 1);
if (ctype_space($letters[0])) {
unset($letters[0]);
}
foreach ($letters as $l) {
if (ctype_space($l)) {
echo "<p> </p>";
} else {
echo "<p>$l</p>";
}
}
$i++;
}
总结一下:我想在“x”个字符之后分割字符串,而不是在“.”上分割字符串就像现在一样
//编辑:
代码在标点符号处分割文本,但我希望它在“x”个字符之后分割
现在它像这样 split :
This is a sentence. This is another sentence. This is a third sentence.
但我希望它像这样分割:
This is a sent ence. This is another senten ce. This is a third sentence .
这里它在 14 个字符(包括空格)后分割。
最佳答案
如果我理解正确的话,这会起作用。替换此代码:
while ($row = mysqli_fetch_array($result)) {
$textline = explode(".", $row["text"]);
}
这样:
while ($row = mysqli_fetch_array($result)) {
$string = $row['text']; $string = wordwrap($string, 80, ";;;;", true);
$oppgavelinjer = explode(";;;;", $string);
}
只要文本不连续包含 4 个分号,就可以将“80”替换为您想要的长度
关于php - x 个字符之后的函数explode(),而不是标点符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23974956/