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

报告相同问题?