|
-
May 20th, 2006, 03:53 AM
#1
character count
hi everyone,
I want to find the character count of a document file.I used the code below.
$lines = file('E:/FILELIST.DOC');
$tot=0;
foreach ($lines as $line_num => $line)
{
echo $line_num."=".$line."<br>";
$tot=$tot+strlen($lines[$line_num])-2;
}
echo $tot;
This code worked correctly at first.But now i got the ouput like this.
0=ÐÏ*¡±á>þÿ 35ÿÿÿÿ Code guru (i have deleted the remaining symbols and characters bcoz it is too long).
The count shown is28648 .actual character count is 8.
why does this happen?is anyother way to find the charater count?
-
May 20th, 2006, 08:56 AM
#2
Re: character count
I wonder, in which language is the document you're trying to count? It might be a language issue. If so, you might want to check mb_strlen() function of PHP.
A few friendly reminders: * Use Code Tags when posting code.
* Rate good replies/post by clicking "Rate this Post" and leaving a positive feedback. * Things should be made as simple as possible, but not any simpler. -- Albert Einstein
-
May 21st, 2006, 10:23 PM
#3
Re: character count
hi friends,
Thanks cherish.
I think it is not a language problem.Language i used is English.The symbols and characters shown as output includes the properties of the .doc file.
I think the header and footer are also included. At first it worked well and i got the correct count.When i check the same in a .txt file i got the correct count. But i need the count in a .doc file. Is there any way to solve this problem
-
May 21st, 2006, 10:33 PM
#4
Re: character count
 Originally Posted by anissurendran
But i need the count in a .doc file. Is there any way to solve this problem
If the .doc file is done in Microsoft Word or another application that uses rich text base, you will not be able to get the count that simply. I am not even sure if you can get it at all. If it is rich text based then there is a lot of extra coding there.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
May 22nd, 2006, 03:25 AM
#5
Re: character count
One possible way is do what you did but have an array of accepted characters i.e. 'a','b','c' etc, and only increment the counter when they are shown. the only trouble is they may be present in stuff you dont want. i also found http://www.phpwordlib.motion-bg.com/
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
-
June 2nd, 2006, 01:54 AM
#6
Re: character count
hihi...i hope that everyone here haven't given up hope on finding new ways for this problem...at least i haven't...hehe...
I have come up with the following function...hope that i got it right
PHP Code:
<?
function adv_count_words($str) {
$words = 0;
// I've replace all double space characters with a single space character and the script not to count spaces as word.
$str = eregi_replace(" +", " ", $str);
// Break the string into pieces that are separated by spaces and place them into an array
$array = explode(" ", $str);
// For every string in the array, i make one more test to assure it is a word.
for($i=0;$i < count($array);$i++) {
if (eregi("[0-9A-Za-z]", $array[$i]))
$words++;
}
return $words;
}
?>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|