PHP获取word文档字数的问题
今天碰到一个需求,是校对论文按字数来计算价格,难点就在统计word文档的字数。一开始的想法是直接用一些第三方插件包例如phpword,然后发现文档和源码中并没有相对应的方法。后来我就用phpword提取出文本的内容,再从网上找了一个仿word统计字数的方法。代码如下:
//先试用comoser下载phpword
composer require phpoffice/phpword
//代码中引入
use PhpOffice\PhpWord\IOFactory;
//读一个文件
$file = '/admin/20200523/d99841cf6d7c4e3a729d66409c434b1a.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;
}
}