I am wondering if there is an elegant way to trim some text but while being HTML tag aware?
For example, I have this string:
$data = 'some title text here that could get very long';
And let’s say I need to return/output this string on a page but would like it to be no more than X characters. Let’s say 35 for this example.
Then I use:
$output = substr($data,0,20);
But now I end up with:
some title text here that
which as you can see the closing strong tags are discarded thus breaking the HTML display.
Is there a way around this? Also note that it is possible to have multiple tags in the string for example:
some text here and here
It’s a big function, but seems to do the trick.
(Thanks to Alexander below for finding a new link to this function)