Friday, September 3rd, 2010

Load File Word Document with PHP

16

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

16 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!

  5. Excellent post, thanks!

  6. Excellent post, thanks for the helpful info!

  7. I’m using like follow the code but not working for me, i’m writing this in a php and including another file..

    not working ……. wht is my prob??????????///

  8. admin says:

    May I know What output did you get?

  9. admin says:

    You are welcome

  10. vinay gupta says:

    Nice article.. I am going to bookmark your web site and keep checking you out. Thanks for sharing your blog.

  11. admin says:

    thanks for bookmark

  12. bharat says:

    hey thanks for nice tutorial i used in my download free full games site .

  13. Thanks for nice article and great tips keep doing :)

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.