Мне интересно, есть ли элегантный способ обрезать какой-то текст, но в то время как тег HTML известен?
Например, у меня есть эта строка:
$data = '<strong>some title text here that could get very long</strong>';
И позвольте сказать, что мне нужно вернуть/вывести эту строку на странице, но хотелось бы, чтобы она была не более X символов. Скажем 35 для этого примера.
Затем я использую:
$output = substr($data,0,20);
Но теперь я получаю:
<strong>some title text here that
который, как вы видите, закрывает сильные теги, отбрасывается, тем самым нарушая отображение HTML.
Есть ли способ обойти это? Также обратите внимание, что в строке можно указать несколько тегов:
<p>some text here <strong>and here</strong></p>