Thursday, March 11th, 2010

Load File Word Document with PHP

6

You might even think about. Is PHP able to load Doc files.
The answer …. it could.

Maybe you’ve done it using the following script:

file_get_contents($filename);

The above code will run smoothly for the file type plain text. However, it will be a problem if we file type is a word document.

If we only use the above code to load a  Doc file, then all the tags that should not be displayed, will be displayed. Thus, it would appear chaotic. Texts so that we do not want do not come then we have to convert first. How do I …?

The problem above can be overcome with the following script snippet:

function parseWord($filename) {
$fileHandle = fopen($filename, "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;
}

$text = parseWord($filename);

Maybe there are some words that do not want that still appears. But at least, this can be a little help you.

Good luck … and develop more

Incoming search terms for the article:

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Comments

6 Responses to “Load File Word Document with PHP”
  1. Richard says:

    I tried the code but it doesn’t display any output.

  2. admin says:

    Do you have successfully loaded the document files?
    Try again the script above
    variable $filename is a variable that contains the file content (input)
    variable $text is the variable that contains the output
    To display the output, you can use a script like this:

    echo $text

  3. Smileys says:

    thanks, just what i was looking for!

  4. Jump higher says:

    Will continue to read it as I’m very new to a lot of the points you mention! Thanks though, I’m glad some people share good stuff like this!

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

Comments links could be nofollow free.