1

How can I read .doc or .docx file by php code. I'm using unicode in doc file.

<?php

$filename = 'abc.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)-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 );

    # (((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);

  }
}

?>

It only read English character file content, if the content is vietnamese language it can not read fine.

Robbert
  • 6,481
  • 5
  • 35
  • 61
  • 1
    You mean UTF-8 or what other character set? Probably you might consider taking some open source libs to accomplish that, there are a couple around to read MS Office Docs, try to google for them (I only know about Excel libs currently). Probably this might help also: http://stackoverflow.com/questions/5052292/php-read-and-write-in-ms-word – Axel Amthor May 29 '13 at 05:08
  • Yes I mean that UTF-8. – Nguyen Anh Tuan May 29 '13 at 09:25
  • Be sure that almost everything is configured to UTF-8: Webserver, PHP, Database, Script files encoding etc. and **then** - don't bother. If there are multibyte problems, its not about your routines but about the set up of your entire environment. – Axel Amthor May 29 '13 at 11:29

0 Answers0