用php读取DOC文件
我正在尝试读取php中的用php读取DOC文件,php,Php,我正在尝试读取php中的.doc.docx文件。一切正常。但在最后一行,我得到了可怕的字符。请帮帮我。 这是由某人开发的代码 function parseWord($userDoc) { $fileHandle = fopen($userDoc, "r"); $line = @fread($fileHandle, filesize($userDoc)); $lines = explode(chr(0x0D),$line); $outtext = "
.doc.docx
文件。一切正常。但在最后一行,我得到了可怕的字符。请帮帮我。
这是由某人开发的代码
function parseWord($userDoc)
{
$fileHandle = fopen($userDoc, "r");
$line = @fread($fileHandle, filesize($userDoc));
$lines = explode(chr(0x0D),$line);
$outtext = "";
foreach($lines as $thisline)
{
$pos = strpos($thisline, chr(0x00));
if (($pos !== FALSE)||(strlen($thisline)==0))
{
} else {
$outtext .= $thisline." ";
}
}
$outtext = preg_replace("/[^a-zA-Z0-9\s\,\.\-\n\r\t@\/\_\(\)]/","",$outtext);
return $outtext;
}
$userDoc = "k.doc";
这是截图。
文档文件不可用
尝试一个库,例如()
注意:由于PHPWord更改了主机和功能,此答案已被多次更新。您可以在PHP中读取.docx文件,但无法读取.doc文件。以下是读取.docx文件的代码:
function read_file_docx($filename){
$striped_content = '';
$content = '';
if(!$filename || !file_exists($filename)) return false;
$zip = zip_open($filename);
if (!$zip || is_numeric($zip)) return false;
while ($zip_entry = zip_read($zip)) {
if (zip_entry_open($zip, $zip_entry) == FALSE) continue;
if (zip_entry_name($zip_entry) != "word/document.xml") continue;
$content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
zip_entry_close($zip_entry);
}// end while
zip_close($zip);
//echo $content;
//echo "<hr>";
//file_put_contents('1.xml', $content);
$content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content);
$content = str_replace('</w:r></w:p>', "\r\n", $content);
$striped_content = strip_tags($content);
return $striped_content;
}
$filename = "filepath";// or /var/www/html/file.docx
$content = read_file_docx($filename);
if($content !== false) {
echo nl2br($content);
}
else {
echo 'Couldn\'t the file. Please check that file.';
}
function read\u file\u docx($filename){
$striped_content='';
$content='';
如果(!$filename | |!file_存在($filename))返回false;
$zip=zip\u open($filename);
如果(!$zip |是数值($zip))返回false;
而($zip\u entry=zip\u read($zip)){
如果(zip_entry_open($zip,$zip_entry)==FALSE)继续;
如果(zip\u entry\u name($zip\u entry)!=“word/document.xml”)继续;
$content.=zip_entry_read($zip_entry,zip_entry_filesize($zip_entry));
zip_entry_close($zip_entry);
}//结束时
zip_close($zip);
//echo$内容;
//回声“
”;
//文件内容('1.xml',$content);
$content=str_replace(“”,$content);
$content=str_replace(“”,\r\n“,$content);
$striped_content=strip_标签($content);
返回$striped_内容;
}
$filename=“filepath”;//或/var/www/html/file.docx
$content=read\u file\u docx($filename);
如果($content!==false){
echo nl2br($content);
}
否则{
echo“无法读取该文件。请检查该文件。”;
}
我正在使用这个功能,对我来说效果很好:)试试吧
function read_doc_file($filename) {
if(file_exists($filename))
{
if(($fh = fopen($filename, 'r')) !== false )
{
$headers = fread($fh, 0xA00);
// 1 = (ord(n)*1) ; Document has from 0 to 255 characters
$n1 = ( ord($headers[0x21C]) - 1 );
// 1 = ((ord(n)-8)*256) ; Document has from 256 to 63743 characters
$n2 = ( ( ord($headers[0x21D]) - 8 ) * 256 );
// 1 = ((ord(n)*256)*256) ; Document has from 63744 to 16775423 characters
$n3 = ( ( ord($headers[0x21E]) * 256 ) * 256 );
// 1 = (((ord(n)*256)*256)*256) ; Document has from 16775424 to 4294965504 characters
$n4 = ( ( ( ord($headers[0x21F]) * 256 ) * 256 ) * 256 );
// Total length of text in the document
$textLength = ($n1 + $n2 + $n3 + $n4);
$extracted_plaintext = fread($fh, $textLength);
// simple print character stream without new lines
//echo $extracted_plaintext;
// if you want to see your paragraphs in a new line, do this
return nl2br($extracted_plaintext);
// need more spacing after each paragraph use another nl2br
}
}
}
我也用过它,但对于重音(和单引号,比如“),它会� 相反,我的PDO mySQL不喜欢它,但我最终通过添加
mb_convert_encoding($extracted_plaintext,'UTF-8');
因此,最终版本应为:
function getRawWordText($filename) {
if(file_exists($filename)) {
if(($fh = fopen($filename, 'r')) !== false ) {
$headers = fread($fh, 0xA00);
$n1 = ( ord($headers[0x21C]) - 1 );// 1 = (ord(n)*1) ; Document has from 0 to 255 characters
$n2 = ( ( ord($headers[0x21D]) - 8 ) * 256 );// 1 = ((ord(n)-8)*256) ; Document has from 256 to 63743 characters
$n3 = ( ( ord($headers[0x21E]) * 256 ) * 256 );// 1 = ((ord(n)*256)*256) ; Document has from 63744 to 16775423 characters
$n4 = ( ( ( ord($headers[0x21F]) * 256 ) * 256 ) * 256 );// 1 = (((ord(n)*256)*256)*256) ; Document has from 16775424 to 4294965504 characters
$textLength = ($n1 + $n2 + $n3 + $n4);// Total length of text in the document
$extracted_plaintext = fread($fh, $textLength);
$extracted_plaintext = mb_convert_encoding($extracted_plaintext,'UTF-8');
// if you want to see your paragraphs in a new line, do this
// return nl2br($extracted_plaintext);
return ($extracted_plaintext);
} else {
return false;
}
} else {
return false;
}
}
这在utf8\u general\u ci mySQL数据库中可以很好地读取word文档文件:)
希望这对其他人有所帮助纯PHP解码从未对我起作用,因此我的解决方案如下: 安装软件包
sudo apt-get install wv
在PHP中使用它:
$output = str_replace('.doc', '.txt', $filename);
shell_exec('/usr/bin/wvText ' . $filename . ' ' . $output);
$text = file_get_contents($output);
# Convert to UTF-8 if needed
if(!mb_detect_encoding($text, 'UTF-8', true))
{
$text = utf8_encode($text);
}
unlink($output);
我正在使用软件将文档转换为txt并读取txt转换文件
soffice --convert-to txt test.doc
您可以在中看到更多信息,.docx是一个.zip压缩的多文件包files@Steve-o我想数一数文档文件中的所有单词。PHPword有可能吗?另外,请注意,PHPword不是由Microsoft编写的:它是由一群独立开发人员编写的免费开源库,不受MS的任何支持或支持。请注意,PHPword现在可以读取BIFF格式的.doc文件以及OfficeOpenXML.docx文件,还有许多其他格式也注意到PHPWord现在正式托管在github上,而不是CodePlex上,是否设法解决了这个@no_freedom?我试过了,但结果是部分文件。欢迎继续,在这里,解释为什么使用您的解决方案,而不仅仅是如何使用,这是一个很好的实践。这将使你的答案更有价值,并有助于进一步的读者更好地理解你是如何做到这一点的。我还建议你看看我们的常见问题:。谢谢你的回答,但如何写入该文件?@user1817444它只是从doc读取文本,如何使用它获取图像?作为二进制数据的图像也可以使用此函数读取文档文件,但我猜只有UTF编码的文件。你能告诉我为什么其他编码不起作用吗?我试着用这个函数来读取一些文件,但它并不适用于所有文件。我看到的唯一区别是编码。请参阅下面我的答案,了解编码问题在搜索简单答案半小时后读取文档文件。我终于得到了这个答案,解决了我的问题。非常感谢@hugsbrugs这是让它按我所希望的那样工作的唯一方法:)