php计算word文档字符数
2021-12-25 13:22:40
$file = '/fc.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();
}
}
}
}
// echo $word;
//计算字数
$word_count = $this->comment_count_word($word);
//计算字数的方法(来源:https://learnku.com/articles/37939)
echo $word_count;
public 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;
}
}
中间报错Class 'ZipArchive' not found
处理办法:
yum install -y php73-php-zip
service php73-php-fpm restart
$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();
}
}
}
}
// echo $word;
//计算字数
$word_count = $this->comment_count_word($word);
//计算字数的方法(来源:https://learnku.com/articles/37939)
echo $word_count;
public 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;
}
}
中间报错Class 'ZipArchive' not found
处理办法:
yum install -y php73-php-zip
service php73-php-fpm restart