【问题标题】:Changing .php to fix excerpt length to allow for Chinese更改 .php 以修复摘录长度以允许中文
【发布时间】:2016-11-16 02:52:34
【问题描述】:

想知道是否有人快速知道如何替换它以允许使用中文,因为中文不会像英语那样被破坏。这是 wordpress 函数 php 的一部分,我正在自定义。我尝试了几种不同的方法,但收效甚微。

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

/**

这适用于英语语言,但需要对其进行调整以允许中文文本摘录。

【问题讨论】:

    标签: javascript php wordpress


    【解决方案1】:

    我觉得$unit != "letter"的时候很难处理,因为中文不是简单的用空格隔开的(" ")。

    对于字母模式, 你可以尝试替换

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

    【讨论】:

    • 感谢您的建议,我正在考虑类似的事情,但无法让它发挥作用。然后我决定重写它。
    【解决方案2】:

    我已经找到了适合 wordpress 的解决方案。我已添加到functions.php。让我知道是否有其他人找到了更好的方法来做到这一点!感谢您的帮助。

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

    【讨论】:

      【解决方案3】:

      我之前在 WordPress forums 上帮助过某人解决了这个问题。这是我在此处发布的解决方案,也许将其发布在 Stack Overflow 上会帮助更多的人。


      这里有一个简单的过滤器来解决这个问题。首先是检测 post excerpt 是否真的是用中文写的,如果是,我们想使用 PHP 提供的一对多字节字符串函数。请记住,过滤器不会影响英文帖子。

      第一个函数是 mb_strlen,我们将使用它来检查摘录是否超出了我们的限制,如果是,我们想在字符串的末尾添加一个装饰器。第二个函数是mb_substr,它将实际字符串限制在我们在函数顶部定义的范围内。

      下面是一个示例过滤器。

      请记住,您的主题需要使用 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');
      

      【讨论】:

        猜你喜欢
        • 2015-08-26
        • 2012-02-04
        • 1970-01-01
        • 1970-01-01
        • 2013-05-29
        • 1970-01-01
        • 2023-03-12
        • 2020-11-01
        • 2010-09-17
        相关资源
        最近更新 更多