PHP substrate but keep HTML tags?

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)

Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 前端开发 » PHP substrate but keep HTML tags?

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录