简体   繁体   English

更改.php以修复摘录长度以允许中文

[英]Changing .php to fix excerpt length to allow for Chinese

wondering if anyone out there has the quick know how to replace this to allow for the use of the chinese language as chinese is not broken the same way as english is. 想知道是否有人能快速知道如何替换它以允许使用中文,因为中文与英语的用法不同。 This is part of a wordpress functions php, im customizing. 这是wordpress函数php的一部分,即时消息自定义。 I have tried several different things with little success. 我尝试了几种不同的方法,但收效甚微。

 */
function cleanead_truncate( $str, $length = 40, $units = 'letters', $ellipsis = ' …' ) {
    if ( $units == 'letters' ) {
        if ( mb_strlen( $str ) > $length ) {
            return mb_substr( $str, 0, $length ) . $ellipsis;
        } else {
            return $str;
        }
    } else {
        $words = explode( ' ', $str );
        if ( count( $words ) > $length ) {
            return implode( " ", array_slice( $words, 0, $length ) ) . $ellipsis;
        } else {
            return $str;
        }
    }
}

if ( ! function_exists( 'cleanead_excerpt' ) ) {
    function cleanead_excerpt( $limit = 40 ) {
      return cleanead_truncate( get_the_excerpt(), $limit, 'words' );
    }
}

/**

This works well for the English Language, but need it adjusted to allow for chinese text excerpt as well. 这对于英语来说效果很好,但也需要对其进行调整以允许中文摘录。

I think it's hard to handle when $unit != "letter" , because chinese word is not simply separated by space ( " " ). 我认为$unit != "letter"时很难处理,因为中文单词不是简单地用空格( " " )分隔。

For the letter mode, You can try to replace 对于字母模式,您可以尝试替换

  • mb_strlen( $str ) to mb_strlen( $str, "utf-8" ) mb_strlen( $str )mb_strlen( $str, "utf-8" )
  • mb_substr( $str, 0, $length ) to mb_substr( $str, 0, $length, "utf-8" ) mb_substr( $str, 0, $length )mb_substr( $str, 0, $length, "utf-8" )

I have figured out an appropriate solution for wordpress. 我已经找到了合适的解决方案WordPress。 which i have added to the functions.php. 我已经添加到functions.php。 Let me know if anyone else has found a better way to do this! 让我知道是否还有其他人找到了更好的方法! Thanks for your help. 谢谢你的帮助。

function dez_filter_chinese_excerpt( $output ) {
global $post;
//check if its chinese character input
$chinese_output = preg_match_all("/\p{Han}+/u", $post->post_content, $matches);
if($chinese_output) {
$output = mb_substr( $output, 0, 50 ) . '...';
}
return $output;
}
add_filter( 'get_the_excerpt', 'dez_filter_chinese_excerpt' );

I helped someone with this problem on the WordPress forums before. 我之前曾在WordPress 论坛上帮助过这个问题的人。 This is the solution I posted there, maybe posting it here on Stack Overflow will help more people in the future. 这是我在此处发布的解决方案,也许将其发布在Stack Overflow上将会在将来帮助更多的人。


Here is a simple filter to fix this problem. 这是解决此问题的简单过滤器。 The First thing is to detect if the post excerpt is in fact written in Chinese and if so we want to use a couple of multi-byte string function provided by PHP. 第一件事是检测帖子摘录是否实际上是用中文编写的,如果是的话,我们想使用PHP提供的几个多字节字符串函数。 Keep in mind the filter will not effect English posts. 请记住,过滤器不会影响英文帖子。

The first function is mb_strlen and we will use this to check if the excerpt exceeds our limit and if so we would like to add a decorator to the end of the string. 第一个函数是mb_strlen ,我们将使用它来检查摘录是否超出了我们的限制,如果是,我们想在字符串的末尾添加装饰器。 The second function is mb_substr and it will limit the actual string to the limit we defined in the top of the function. 第二个函数是mb_substr ,它将实际字符串限制为我们在函数顶部定义的限制。

Below is an example filter. 以下是示例过滤器。

Keep in mind your theme needs to use the get_the_excerpt() method and the filter will not effect English posts . 请记住,您的主题需要使用get_the_excerpt()方法,并且过滤器不会影响英文帖子

/**
 * Filters the excerpt to limit Chinese strings properly.
 *
 * @return string The new limited excerpt
 */
function filter_chinese_excerpt($excerpt){

    // Chinese excerpt limit. Set your limit here!
    $limit = 130;

    // Ending decoration. ([...])
    $decoration = '[…]';

    // If Chinese.
    if(preg_match("/\p{Han}+/u", $excerpt)){

        // If longer then limit add decoration to end of string.
        // Also returns the string
        if(mb_strlen($excerpt, 'UTF-8') >= $limit){
             return mb_substr($excerpt, 0, $limit, 'UTF-8') . $decoration;
         }else{
             return mb_substr($excerpt, 0, $limit, 'UTF-8');
         }
    }
    return $excerpt;
}
add_filter('get_the_excerpt', 'filter_chinese_excerpt');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM