CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: character count

  1. #1
    Join Date
    Mar 2006
    Posts
    12

    Unhappy 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?

  2. #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

  3. #3
    Join Date
    Mar 2006
    Posts
    12

    Arrow 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

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: character count

    Quote 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.

  5. #5
    Join Date
    Aug 2005
    Location
    southampton, UK
    Posts
    1,320

    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

  6. #6
    Join Date
    Sep 2002
    Location
    All around the World
    Posts
    99

    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
  •  





Click Here to Expand Forum to Full Width

Featured