How to limit word to show

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to limit word to show

hendisantika
Hi All,

I want to ask something about best practice or good way to do this issue:
How We Do to limit word to show?
i.e We have a sentence like : "Thymeleaf is the best template engine for Spring Boot HTML".

That is 10 words. I want to show only 4 words only which is going to be like this:
"Thymeleaf is the best".

Does somebody know how to do it in thymeleaf?

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: How to limit word to show

alfred0809
This post was updated on .
Use this code to solve the problem
$string = strip_tags($string);
if (strlen($string) > 500) {

    // truncate string VidMate TeaTV Shareit
    $stringCut = substr($string, 0, 500);
    $endPoint = strrpos($stringCut, ' ');

    //if the string doesn't contain any space then it will cut without word basis.
    $string = $endPoint? substr($stringCut, 0, $endPoint) : substr($stringCut, 0);
    $string .= '... Read More';
}
echo $string;