doulou1989 2019-03-27 18: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.

<?php
    foreach($stories as $story) {
        echo '<h2><a href="'.BASE_URL.'/'.$story->slug.'">'.limit_text($story->title, 80).'</a></h2>';
        if(!empty($story->excerpt)) {
            echo '<p>'.limit_text($story->excerpt, 150).'</p>';
        } else {
            echo limit_text($story->body, 150);
        }
    }
    ?>

I found the function for limit_text

function limit_text($string, $limit = 140) {
$string = preg_replace('/<figcaption>.*?<\/figcaption>/','',$string);
$string = preg_replace('/<div class=\"wp_image_caption\">.*?<\/div>/','',$string);
$string = str_replace('&nbsp;','',$string);

$string = substr($string, strpos($string, "<p><strong>"));
$string = strip_tags($string);
$string = substr($string, 0, $limit);
$string = $string.'...';
return $string;
}
  • 写回答

2条回答 默认 最新

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

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

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

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler