mb_substr切断文章中的文字摘录? [英] mb_substr cutting off words in post excerpt?

查看:164
本文介绍了mb_substr切断文章中的文字摘录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

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



$ b $ / code $ / pre

我编辑 mb_substr ,以便摘录将在一段时间结束?例如,一个片段会像这样结束她有一个可爱的无辜的魅力,而不是她有一种可爱的无辜的魅力,让人们关心她。我想要摘录完成句子,并在一段时间内结束,而不是只停留在字符数限制,设置为800.在代码$ ln设置为800.



更新:

这是我最终使用的代码。单词在摘录中仍然被截断,但是我添加了一个Read More链接到带有点的结尾。

  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>
} 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


$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ <解决方案

如果你能保证会有一个。在你的文本,然后你可以做到这一点:$ b​​
$ b $ pre $ echo $($) amet egestas lacus。Maecenas posuere dolor vitae ultrices viverra。,。);

这会输出:Lorem ipsum dolor sit amet,consectetur adipiscing elit



strtok手册页


This is the code I am using to show html formatting on my wordpress post excerpts:

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

    }

How can I edit the mb_substr so that the excerpt will end at a period? For instance, an excerpt will end like this "she has a lovable and innocent charm tha" instead of "she has a lovable and innocent charm that keeps people caring about her." I want the excerpt to finish the sentence and end in a period instead of only stopping at the character limit, which is set at 800. In the code $ln is set to 800.

UPDATE:

This is the code I ended up using. Words are still cut off in the excerpt but I added a Read More link to the end with dots.

 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 } 
    }

}

解决方案

If you can guarantee that there will be a "." in your text then you can just do this:

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

This will output: "Lorem ipsum dolor sit amet, consectetur adipiscing elit"

strtok manual page

这篇关于mb_substr切断文章中的文字摘录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆