ohdang888 Posted September 28, 2010 Share Posted September 28, 2010 I am running PHP 5 on a linux server. I want to be able to open a .doc or .docx file with PHP (or any way possible, for that matter) in order to grab the contents from it. How can i do this? I know COM classes work with windows for this, but i have linux. Any ideas are greatly appreciated. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/214661-open-and-get-contents-of-a-word-document-with-php/ Share on other sites More sharing options...
Rifts Posted September 28, 2010 Share Posted September 28, 2010 I just googled this I have no idea if it works or not. <?php $filename = './12345.doc'; 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)-*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 ); # (((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); # if you want the plain text with no formatting, do this echo $extracted_plaintext; # if you want to see your paragraphs in a web page, do this echo nl2br($extracted_plaintext); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/214661-open-and-get-contents-of-a-word-document-with-php/#findComment-1116926 Share on other sites More sharing options...
ohdang888 Posted September 29, 2010 Author Share Posted September 29, 2010 thanks for responding! That script works, but it doesn't preserve formatting (underlines, bold, etc.) Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/214661-open-and-get-contents-of-a-word-document-with-php/#findComment-1116976 Share on other sites More sharing options...
Rifts Posted September 29, 2010 Share Posted September 29, 2010 what happens if you remove this line from the code echo $extracted_plaintext; Quote Link to comment https://forums.phpfreaks.com/topic/214661-open-and-get-contents-of-a-word-document-with-php/#findComment-1117011 Share on other sites More sharing options...
ohdang888 Posted September 29, 2010 Author Share Posted September 29, 2010 what happens if you remove this line from the code echo $extracted_plaintext; Ya i played around with it. It doesn't have the formatting at all. Quote Link to comment https://forums.phpfreaks.com/topic/214661-open-and-get-contents-of-a-word-document-with-php/#findComment-1117325 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.