Answer the question
In order to leave comments, you need to log in
how to split text into 5 parts php?
...
$seotext = $text;
...
Answer the question
In order to leave comments, you need to log in
If you want to get paragraphs of approximately the same size:
$text = '...';
$textSize = mb_strlen($text);
$paragraphsCount = 5;
$averageParagpaphSize = ceil($textSize / $paragraphsCount);
$paragraphs = [];
for ($i = 0; $i < $paragraphsCount; $i++)
{
$currentParagraph = mb_substr($text, $averageParagpaphSize * $i, $averageParagpaphSize);
$paragraphs[] = $currentParagraph;
}
for ($i = 1; $i < $paragraphsCount; $i++)
{
$currentParagraph = $paragraphs[$i - 1];
$nextParagraph = $paragraphs[$i];
if (mb_substr($currentParagraph, -1, 1) !== '.')
{
$dotPosition = mb_strpos($nextParagraph, '.') + 1;
$currentParagraph .= mb_substr($nextParagraph, 0, $dotPosition);
$nextParagraph = trim(mb_substr($nextParagraph, $dotPosition));
}
$paragraphs[$i - 1] = $currentParagraph;
$paragraphs[$i] = $nextParagraph;
}
var_dump($paragraphs);
$text = '...';
$paragraphsCount = 5;
$sentences = mb_split('\.', $text);
$paragraphs = array_chunk($sentences, ceil(count($sentences) / $paragraphsCount));
array_walk($paragraphs, function (&$paragraphSentences) {
$paragraphSentences = implode(' ', $paragraphSentences);
});
var_dump($paragraphs);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question