迈出脚步
体验世界

php 统计word文档字数

先试用comoser下载phpword

composer require phpoffice/phpword

//代码中引入

use PhpOffice\PhpWord\IOFactory;

//读一个文件

$file = '/document/myword.docx';
$sections = IOFactory::load($file)->getSections();

//提取文本内容(过滤掉图片)

$word = '';
foreach($sections as $section) {
    $elements = $section->getElements();
    foreach($elements as $element) {
        if (!method_exists($element, 'getElements')) {
            continue;
        }
        foreach ($element->getElements() as $item) {
            if ($item instanceof \PhpOffice\PhpWord\Element\Text) {
                $word .= $item->gettext();
            }
        }
    }
}

//计算字数

$word_count = comment_count_word($word);

//计算字数的方法(来源:https://learnku.com/articles/37939)

function comment_count_word($str){
    //$str =characet($str);
    //判断是否存在替换字符
    $is_tihuan_count=substr_count($str,"龘");
    try {
        //先将回车换行符做特殊处理
        $str = preg_replace('/(\r\n+|\s+| +)/',"龘",$str);
        //处理英文字符数字,连续字母、数字、英文符号视为一个单词
        $str = preg_replace('/[a-z_A-Z0-9-\.!@#\$%\\\^&\*\)\(\+=\{\}\[\]\/",\'<>~`\?:;|]/',"m",$str);
        //合并字符m,连续字母、数字、英文符号视为一个单词
        $str = preg_replace('/m+/',"*",$str);
        //去掉回车换行符
        $str = preg_replace('/龘+/',"",$str);
        //返回字数
        return mb_strlen($str)+$is_tihuan_count;
    } catch (Exception $e) {
        return 0;
    }
}

赞(0) 喜欢我
未经允许不得转载:王威 » php 统计word文档字数
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏