Search code examples
javascriptphphtmlmysqlckeditor

How to display short information in list content?


I have already made data in my database. It made from ckeditor5. It looks like here enter image description here

The data mentions many tags and attributes. so... I just need to get the words inside tag p only. So I can display on my website. The example of the result is here: enter image description here

Note: I just need to show 100 letters. And add '...' in the last sentence.

My last code looks like this:

function removeTags(str) {
  if ((str === null) || (str === ''))
    return false;
  else
    str = str.toString();

  // Regular expression to identify HTML tags in  
  // the input string. Replacing the identified  
  // HTML tag with a null string. 
  return str.replace(/(<([^>]+)>)/ig, '');
}
<?php
while ($row = $result->fetch_array(MYSQLI_ASSOC)){

echo '
<script>
  document.write(removeTags(
    '.$row['main_article'].'));
</script>';

}
?>


Solution

  • For truncating text, you can use this function.

    function truncateString(str, num) {
    // str = character length
      if (str.length <= num) {
        return str
      }
      return str.slice(0, num) + '...'
    }
    
    truncateString("A-tisket a-tasket A green and yellow basket", 8);
    

    Source: https://medium.com/@DylanAttal/truncate-a-string-in-javascript-41f33171d5a8