doulou1989 2019-03-27 10:28
浏览 93
已采纳

limit_text仅输出纯文本而不显示粗体字体

I have some code that outputs a title and a description for a list of posts. I am trying to limit the length of text for both. The title is $story->title, and outputs just fine. However the description, represented by $story->excerpt in the code, contains html tags in the database. The limit_text function seems to strip these tags from the text. I think I need to limit the characters differently or need a function to allow those tags to work.

I have tried some functions that allows the tags to be seen but not function properly. But I am new to php in general so I don't know many functions.

  1. <?php
  2. foreach($stories as $story) {
  3. echo '<h2><a href="'.BASE_URL.'/'.$story->slug.'">'.limit_text($story->title, 80).'</a></h2>';
  4. if(!empty($story->excerpt)) {
  5. echo '<p>'.limit_text($story->excerpt, 150).'</p>';
  6. } else {
  7. echo limit_text($story->body, 150);
  8. }
  9. }
  10. ?>

I found the function for limit_text

  1. function limit_text($string, $limit = 140) {
  2. $string = preg_replace('/<figcaption>.*?<\/figcaption>/','',$string);
  3. $string = preg_replace('/<div class=\"wp_image_caption\">.*?<\/div>/','',$string);
  4. $string = str_replace('&nbsp;','',$string);
  5. $string = substr($string, strpos($string, "<p><strong>"));
  6. $string = strip_tags($string);
  7. $string = substr($string, 0, $limit);
  8. $string = $string.'...';
  9. return $string;
  10. }
  • 写回答

2条回答 默认 最新

  • dsdt66064367 2019-03-27 10:56
    关注

    The reason why my tags were being stripped was the strip_tagsmethod right in the function. Duh. Hehe. Thanks for the comments.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部