mb_substr删掉了文章节选中的单词



这是我用来在wordpress文章摘录中显示html格式的代码:

 function get_excerpt($post, $ln){
        if (!empty($post->post_excerpt)) {
            if (strlen($post->post_excerpt) > $ln) {
                echo mb_substr(($post->post_excerpt), 0, $ln);
            } 
        } else {
            if (strlen($post->post_content) > $ln) {
                  echo mb_substr(($post->post_content), 0, $ln);
            } 
        }
    }

如何编辑mb_substr,使摘录在某个时间段结束?例如,一段摘录的结尾是"她有一种可爱而天真的魅力tha",而不是"她有让人们关心她的可爱和天真的魅力"。在代码中$ln被设置为800。

更新:

这就是我最终使用的代码。摘录中的文字仍然被剪掉,但我在结尾添加了一个"阅读更多"链接,并用点表示。

 function get_excerpt($post, $ln){
    if (!empty($post->post_excerpt)) {
        if (strlen($post->post_excerpt) > $ln) {
            echo mb_substr(($post->post_excerpt), 0, $ln) . '...'; ?> <a href="<?php echo get_permalink( $post -> ID )?>">[Read More]</a>
 <?php } 
    } else {
        if (strlen($post->post_content) > $ln) {
              echo mb_substr(($post->post_content), 0, $ln) . '...';?> <a href="<?php echo get_permalink( $post -> ID )?>">[Read More]</a>
 <?php } 
    }
}

如果你能保证你的文本中会有一个".",那么你可以这样做:

echo strtok("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam sit amet egestas lacus. Maecenas posuere dolor vitae ultrices viverra.",".");

这将输出:"Lorem ipsum悲哀坐amet,consectetur adipiscing elit"

strtok手动页面

最新更新