Truncate text while preserving HTML tags

Submitted by:Jhon Jhon

Date added:20 September, 2014

Category:PHP

Truncate or limit a text string while still preserving any HTML tags present. The code auto-closes the tags.

Tags: truncate text , text string

Code Snippet:

ECHO String::truncate('jo<i><b>n</b>as</i>', 3, '...'); //jo<...
ECHO String::truncate('jo<i><b>n</b>as</i>', 3, '...', TRUE); //jo<i><b>n</b></i>...
ECHO String::truncate('jo<i><b>n</b>as</i>', 3, '...', TRUE, FALSE); //jo<i><b>n...
 
 

Comments