Skip to content

Instantly share code, notes, and snippets.

@PululuK
Last active February 23, 2022 12:51
Show Gist options
  • Save PululuK/74f4634e17da564b0f6cfcd7f23c3d2e to your computer and use it in GitHub Desktop.
Save PululuK/74f4634e17da564b0f6cfcd7f23c3d2e to your computer and use it in GitHub Desktop.
<?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;
}
@PululuK
Copy link
Author

PululuK commented Feb 23, 2022

<?php

$t = "Le lorem ipsum est, en imprimerie, une suite de mots sans signification utilisée à titre provisoire pour calibrer une mise en page, le texte définitif venant remplacer le faux-texte dès qu'il est prêt ou que la mise en page est achevée. Généralement, on utilise un texte en faux latin, le Lorem ipsum ou Lipsum.";

var_dump(seemoreFormatter($t, 1));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment