gerkintrigg Posted August 30, 2008 Share Posted August 30, 2008 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 More sharing options...
asmith Posted August 30, 2008 Share Posted August 30, 2008 Have you tried str_word_count ? It will ignore some character. Just a shot Link to comment https://forums.phpfreaks.com/topic/121969-microsoft-word-word-count/#findComment-629616 Share on other sites More sharing options...
gerkintrigg Posted August 31, 2008 Author Share Posted August 31, 2008 ooh interesting. thanks for that! I'll let you know. Link to comment https://forums.phpfreaks.com/topic/121969-microsoft-word-word-count/#findComment-630273 Share on other sites More sharing options...
gerkintrigg Posted August 31, 2008 Author Share Posted August 31, 2008 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. Link to comment https://forums.phpfreaks.com/topic/121969-microsoft-word-word-count/#findComment-630276 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.