摘录();函数未计算/工作日语单词

问题描述 投票:1回答:2

我正在日语网站上工作,并且将此代码用于单词限制,当我粘贴英语句子但不使用日语单词时,它可以正常工作。

function content($num) {
$theContent = get_the_content();
$output = preg_replace('/<img[^>]+./','', $theContent);
$output = preg_replace( '/<blockquote>.*<\/blockquote>/', '', $output );
$output = preg_replace( '|\[(.+?)\](.+?\[/\\1\])?|s', '', $output );
$limit = $num+1;
$content = explode(' ', $output, $limit);
array_pop($content);
$content = implode(" ",$content)."...";
  echo $content;
}


<?php content('15'); ?>

任何人都可以帮助我,而且一件事是我正在使用xeory_extension主题。

javascript php jquery wordpress plugins
2个回答
0
投票

问题是日语字符为多字节(平假名和片假名字符存储在UTF-8中的3个字节上,因此您必须使用special php multibytes string functions来操纵包含日语字符的字符串。

可悲的是,PHP没有提供开箱即用的mb_explode函数。尽管有人为此进行了努力,但使用mb_strlenmb_substr来建立该缺少的功能。

以下代码是我的笔记,它来自the fetus-hina mb_explode gist

function mb_explode($delimiter, $string, $limit = -1, $encoding = 'auto') {
    if(!is_array($delimiter)) {
        $delimiter = array($delimiter);
    }
    if(strtolower($encoding) === 'auto') {
        $encoding = mb_internal_encoding();
    }
    if(is_array($string) || $string instanceof Traversable) {
        $result = array();
        foreach($string as $key => $val) {
            $result[$key] = mb_explode($delimiter, $val, $limit, $encoding);
        }
        return $result;
    }

    $result = array();
    $currentpos = 0;
    $string_length = mb_strlen($string, $encoding);
    while($limit < 0 || count($result) < $limit) {
        $minpos = $string_length;
        $delim_index = null;
        foreach($delimiter as $index => $delim) {
            if(($findpos = mb_strpos($string, $delim, $currentpos, $encoding)) !== false) {
                if($findpos < $minpos) {
                    $minpos = $findpos;
                    $delim_index = $index;
                }
            }
        }
        $result[] = mb_substr($string, $currentpos, $minpos - $currentpos, $encoding);
        if($delim_index === null) {
            break;
        }
        $currentpos = $minpos + mb_strlen($delimiter[$delim_index], $encoding);
    }
    return $result;
}

然后以与爆炸相同的方式使用它:

$content = mb_explode(' ', $output, $limit);

对于implodeyou shouldn't have any issue


0
投票

这对我有用

    function custom_short_excerpt($excerpt){
    $limit = 200;

    if (strlen($excerpt) > $limit) {
        return substr($excerpt, 0, strpos($excerpt, ' ', $limit));
    }

    return $excerpt;
}

add_filter('the_excerpt', 'custom_short_excerpt');
© www.soinside.com 2019 - 2024. All rights reserved.