Limiting String Length in PHP


Trim a string to a specified length and append “…” if it’s too long.

Source Code

function limitString($string, $limit = 100) {
    return strlen($string) > $limit ? substr($string, 0, $limit) . "..." : $string;
}
echo limitString("This is a very long string that needs to be trimmed.", 25);
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments