Last active
February 23, 2022 12:51
-
-
Save PululuK/74f4634e17da564b0f6cfcd7f23c3d2e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function seemoreFormatter(string $text, int $wordsLimit = 250):array{ | |
$textInfos = [ | |
'text' => $text, | |
'see_more' => null, | |
]; | |
$words = str_word_count($text, 1); | |
if(count($words) < $wordsLimit) { | |
return $textInfos; | |
} | |
$limitedWords = array_filter($words, function($key) use ($wordsLimit) { | |
return $key < $wordsLimit; | |
}, ARRAY_FILTER_USE_KEY); | |
$seeMoreWords = array_filter($words, function($key) use ($wordsLimit) { | |
return $key >= $wordsLimit; | |
}, ARRAY_FILTER_USE_KEY); | |
$textInfos['text'] = implode(' ', $limitedWords); | |
$textInfos['see_more'] = implode(' ', $seeMoreWords); | |
return $textInfos; | |
} |
Author
PululuK
commented
Feb 23, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment