简体   繁体   中英

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. 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 ( " " ).

For the letter mode, You can try to replace

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

I have figured out an appropriate solution for wordpress. which i have added to the 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. This is the solution I posted there, maybe posting it here on Stack Overflow will help more people in the future.


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. 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. The second function is mb_substr and it will limit the actual string to the limit we defined in the top of the function.

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 .

/**
 * 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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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