【问题标题】:How to display short information in list content?如何在列表内容中显示简短信息?
【发布时间】:2021-01-31 17:50:44
【问题描述】:

我已经在我的数据库中创建了数据。它由ckeditor5制成。看起来像这里

数据提到了许多标签和属性。所以...我只需要在标签 p 中获取单词。所以我可以在我的网站上显示。结果示例如下:

注意:我只需要显示 100 个字母。并在最后一句中添加“...”。

我最后的代码是这样的:

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>';

}
?>

【问题讨论】:

    标签: javascript php html mysql ckeditor


    【解决方案1】:

    我找到了解决方案。在这里,希望将来有人需要它来获取他的 ckeditor 内容:

    <p><?php 
    
    $str = strip_tags($row['main_article']);
    $str = substr($str, 0, 200) . '...';
    echo $str;
    
    ?></p>

    【讨论】:

      【解决方案2】:

      对于截断文本,您可以使用此功能。

      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);
      

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

      【讨论】:

      • ow.. 对不起,但我刚刚找到了我的解决方案。但我感谢你的努力。谢谢。
      猜你喜欢
      • 2019-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-03
      • 1970-01-01
      • 1970-01-01
      • 2012-10-05
      • 1970-01-01
      相关资源
      最近更新 更多