Jump to content

Microsoft Word - word count


gerkintrigg

Recommended Posts

Hello!

 

I have written a nice little script to count the words in a text file. (code below). it works fine, but now I am trying the same thing with a word document and it's proving a bit of a nightmare due to the word count apparently counting the encrypted elements. Please note I only want to count the words. I'm happy for it to ignore the images and formatting completely.

 

I don't have the COM classes required to open as an MS word document, but are there any programming filters I can use to only get the ASCII?

 

Thanks...

Here is the code for the txt files:

$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['text_file']['name']); 
if(!move_uploaded_file($_FILES['text_file']['tmp_name'], $target_path)) {
    $error= "The file ".  basename( $_FILES['text_file']['name']). 
    " has been uploaded";
}
$my_file= 'uploads/'.basename( $_FILES['text_file']['name']);
$text= fopen('uploads/'.basename( $_FILES['text_file']['name']), "r")or $error.='<BR>File cannot be opened.';
$theData = fread($text, filesize($my_file));
$words=explode(' ', $theData);
$i=0;
foreach($words as $key=>$value){
$i++;
}
fclose($text);
if($i<=1000){
$price=$r['price_2'];
}
if($i<=500){
$price=$r['price_1'];
}

Link to comment
https://forums.phpfreaks.com/topic/121969-microsoft-word-word-count/
Share on other sites

Well it does work with basic text files - and also allows no errors with Excel files or word files but still reads my 1717 word word-file as over 8000...

 

It must be reading something that I don't want it to...

 

Thanks for this... it still cut out a lot of the rubbish and reduced my code too.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.