PHP在linux读取word文档
#wget http://www.winfield.demon.nl/linux/antiword-0.37.tar.gz
#tar zxvf antiword-0.37.tar.gz
#cd antiword-0.37
#make
#make install
antiword
cp /root/bin/*antiword /usr/local/bin/
mkdir /usr/share/antiword
cp -R /root/.antiword/* /usr/share/antiword/
chmod 777 /usr/local/bin/*antiword
chmod 755 /usr/share/antiword/*
安装完成之后,如果要在web上查看的话,需要使用root执行 make global_install
1
2
3
4
5
6
7
8
9
10
11
12
|
<?php header( "Content-type: text/html; charset=utf-8" ); $filename = 'test.doc' ; # $content = shell_exec( '/usr/local/bin/antiword ' . $filename ); $content = shell_exec( 'antiword -mUTF-8 ' . $filename ); echo '<pre>' ; print_r ( $content ); echo '</pre>' ; |
shell上测试使用
-
/usr/local/bin/antiword 你的word文档
-
#如果中文码乱,再加上编码
-
/usr/local/bin/antiword -w 0 -m UTF-8.txt 你的word文档
-
#需要注意,word文档内容太少会提示: I’m afraid the text stream of this file is too small to handle.
php上使用
-
$filename = ‘你的word文档’;
-
$content = shell_exec(‘/usr/local/bin/antiword -w 0 -m UTF-8.txt ‘.$filename);
-
//将串中所有可能的全角符转为半角符
-
//全角
-
$DBC = Array(
-
‘0’ , ‘1’ , ‘2’ , ‘3’ , ‘4’ ,
-
‘5’ , ‘6’ , ‘7’ , ‘8’ , ‘9’ ,
-
‘A’ , ‘B’ , ‘C’ , ‘D’ , ‘E’ ,
-
‘F’ , ‘G’ , ‘H’ , ‘I’ , ‘J’ ,
-
‘K’ , ‘L’ , ‘M’ , ‘N’ , ‘O’ ,
-
‘P’ , ‘Q’ , ‘R’ , ‘S’ , ‘T’ ,
-
‘U’ , ‘V’ , ‘W’ , ‘X’ , ‘Y’ ,
-
‘Z’ , ‘a’ , ‘b’ , ‘c’ , ‘d’ ,
-
‘e’ , ‘f’ , ‘g’ , ‘h’ , ‘i’ ,
-
‘j’ , ‘k’ , ‘l’ , ‘m’ , ‘n’ ,
-
‘o’ , ‘p’ , ‘q’ , ‘r’ , ‘s’ ,
-
‘t’ , ‘u’ , ‘v’ , ‘w’ , ‘x’ ,
-
‘y’ , ‘z’ , ‘-’ , ‘ ’ , ‘:’ ,
-
‘.’ , ‘,’ , ‘/’ , ‘%’ , ‘#’ ,
-
‘!’ , ‘@’ , ‘&’ , ‘(’ , ‘)’ ,
-
‘<’ , ‘>’ , ‘"’ , ‘'’ , ‘?’ ,
-
‘[’ , ‘]’ , ‘{’ , ‘}’ , ‘\’ ,
-
‘|’ , ‘+’ , ‘=’ , ‘_’ , ‘^’ ,
-
‘¥’ , ‘ ̄’ , ‘`’
-
);
-
// 半角
-
$SBC = Array(
-
‘0’, ‘1’, ‘2’, ‘3’, ‘4’,
-
‘5’, ‘6’, ‘7’, ‘8’, ‘9’,
-
‘A’, ‘B’, ‘C’, ‘D’, ‘E’,
-
‘F’, ‘G’, ‘H’, ‘I’, ‘J’,
-
‘K’, ‘L’, ‘M’, ‘N’, ‘O’,
-
‘P’, ‘Q’, ‘R’, ‘S’, ‘T’,
-
‘U’, ‘V’, ‘W’, ‘X’, ‘Y’,
-
‘Z’, ‘a’, ‘b’, ‘c’, ‘d’,
-
‘e’, ‘f’, ‘g’, ‘h’, ‘i’,
-
‘j’, ‘k’, ‘l’, ‘m’, ‘n’,
-
‘o’, ‘p’, ‘q’, ‘r’, ‘s’,
-
‘t’, ‘u’, ‘v’, ‘w’, ‘x’,
-
‘y’, ‘z’, ‘-‘, ‘ ‘, ‘:’,
-
‘.’, ‘,’, ‘/’, ‘%’, ‘#’,
-
‘!’, ‘@’, ‘&’, ‘(‘, ‘)’,
-
‘<‘, ‘>’, ‘”‘, ‘\”,‘?’,
-
‘[‘, ‘]’, ‘{‘, ‘}’, ‘\\’,
-
‘|’, ‘+’, ‘=’, ‘_’, ‘^’,
-
‘$’, ‘~’, ‘`’
-
);
-
$content = str_replace($DBC, $SBC, $str); // 全角到半角
-
var_dump($content);