I am developing a news article web page where i want to separate the long text of a news story in to four part and save them into MSQL DB in four separate column.
This is just to achieve flexibility when presenting it to the readers in such a way that i can place some important information in the middle of the content after some paragraph (less after two or three paragraph). important info such as related news, advert and the like. This is a common practice on many news website.
So far i have tried the code below which rises another issued for me:
function split_words_into_parts( string $words, int $parts = 5 ) {
$words_array = explode( ' ', $words );
$words_cnt = count( $words_array );
$per_part = max( round( $words_cnt / $parts ), 1 );
$result = [];
/*--- Splitting the full array ---*/
for( $i = 0; $i < $parts; $i++ ) {
$result[] = array_slice( $words_array, $i * $per_part, $per_part );
}
$part1 = implode(" ", $result[0]);
$part2 = implode(" ", $result[1]);
$part3 = implode(" ", $result[2]);
$part4 = implode(" ", $result[3]);
$part5 = implode(" ", $result[4]);
if($words_cnt <= 1000) {
return [$part1.' '.$part2.' '.$part3, $part4.' '.$part5, '', ''];
}
if($words_cnt > 1000 AND $words_cnt < 2500) {
return [$part1.' '.$part2, $part3.' '.$part4, $part5, ''];
}
if($words_cnt >= 2500) {
return [$part1.' '.$part2, $part3, $part4, $part5];
}
}
In the code, the text were separated into 5 but its return four array element where the first element contains two part of the separated text because i want the first part to be much than the rest.
The issue with the above code is that it separate base on number of text, which means a full paragraph conveying a particular message in the story might be spilt into different part and an advert will be place in between them making the paragraph confusing and hard to understand in-fact inconvenient reader.
please what I want is splitting the text base on paragraph but still maintaining the raw text from the textarea because i am using php pasedown library
thank you for your anticipated consideration