вырезать текст после html конечного тега

I have a comment system in my website and some users write very long comments, longer than 500 chars and I need to cut it after 200 and add the option "see more". The problem is that users can use <a>test</a> tags and in some cases the limit of 200 chars cuts the tag in the middle , like <a>t or <a or <a>test</ If any of the cases above happens, the limit should extend until the end of the html tag so ex <a>test</a>

I have this code:

function truncate($string,$length=200,$append="&hellip;") {
  $string = trim($string);

  if(strlen($string) > $length) {
    $string = wordwrap($string, $length);
    $string = explode("\n", $string, 2);
    $string = $string[0] . $append;
  }

  return $string;
}

Any idea how to make this?

Thanks

php
7 голосов | спросил zeeks 26 FriEurope/Moscow2014-12-26T13:22:50+03:00Europe/Moscow12bEurope/MoscowFri, 26 Dec 2014 13:22:50 +0300 2014, 13:22:50

0 ответов


Похожие вопросы

Популярные теги

security × 330linux × 316macos × 2827 × 268performance × 244command-line × 241sql-server × 235joomla-3.x × 222java × 189c++ × 186windows × 180cisco × 168bash × 158c# × 142gmail × 139arduino-uno × 139javascript × 134ssh × 133seo × 132mysql × 132