在没有断句的情况下在多个部分中划分长字符串

问题描述 投票:0回答:1

我在php数据库中有很长的字符串,我想在不破坏句子的情况下分成5个部分。如果我的长文本分为4个部分,则剩余部分不显示。

我使用substr(),但没有完整的句子。这是我的PHP代码示例:

$first = substr($story, 0, 1000);
$second = substr($story, 1001, 2000);
$third = substr($story, 2001, 3000);
$fourth = substr($story, 3001, 3800);

这里我附加输出样本以显示2节中的长文本。 My output

请帮我在多个部分划分长字符串而不分节。

php html
1个回答
0
投票

使用wordwrap函数怎么样?与爆炸相结合,您可以将这些块拆分成一个数组,打破字符限制下或下面的单词:

$unique_marker = "**!!**";      // A temporary and unique delimter for the explode
$trim_under = 1000;             // Split chunk under this character limit
$lines = explode($unique_marker, wordwrap($story, $trim_under, $unique_marker, true));
print_r($lines);                // Show us the contents of the array

我没有测试过,但上面的代码应该让你指出正确的方向......

© www.soinside.com 2019 - 2024. All rights reserved.