dpt1712 2015-10-21 06:46
浏览 40
已采纳

摘抄(); 功能不计算/工作日语单词

I am working on Japanese language web site, and using this code for words limit, it is working when I paste English sentence but not working with Japanese words .

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

Can any body help me, and one thing is that I am using xeory_extension theme.

  • 写回答

1条回答 默认 最新

  • dongxin5054 2015-10-21 10:34
    关注

    The problem is that Japanese characters are multibyte (Hiragana and Katakana characters are stored on 3 bytes in UTF-8), so you'll have to use special php multibytes string functions to manipulate strings that contains Japanese characters.

    Sadly PHP doesn't provide a mb_explode function out of the box. Though some people worked on that, using mb_strlen and mb_substr to build up that missing function.

    The following code is note mine, it comes from 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;
    }
    

    Then just use it the same way you use explode:

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

    And for implode, you shouldn't have any issue.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写