Hello all,

I've got a stumper that none of my PHP buddies can answer for me. How can I tell the size of a string (in bytes)? I'm reading in a STDIN from a form so it can contain binary file characters, etc. Just wondering if there is a way to guarantee the size of the string (meaning it needs to take into account unicode or unicode detection).

Thanks!

    [man]strlen[/man]

    See also count(), and mb_strlen().

      From PHP.net: "mb_strlen() returns number of characters in string str having character encoding encoding. A multi-byte character is counted as 1"

      I need to know the size in bytes, not characters. Ie, multi-byte characters will need to be counted as the number of bytes that they actually are.

        then you want [man]count[/man]. alternatively save the string as file, then use [man]filesize[/man]

          I thought about using filesize, but this operation will be happening a lot meaning that I'd like to make it as efficient as possible and creating, modifying, and deleting a temporary file on each iteration probably isn't the best method to do that.

          As for count, according to the page you sent me to, "count--Count elements in an array, or properties in an object"

          It doesn't even seem to work for a string (only arrays and objects). But if it did, I'd imagine it would count the number of characters. Unfortunately, depending on what encoding is used, (UTF-8, etc) I can't be sure how many bytes a character is.

          Any other suggestions?

            Gosh, since you reject every suggestion being made, I think you might have to rely on your own devices. Too bad you don't want to actually do the filesize thing, because it will work and very fast. Thing is, you'd have to actually write some code. Gosh that sucks.

              I think filesize is the only option, however, right now I'm following up on a lead that looks like this:

              $encoding = mb_detect_encoding($newData);
              $currLength += mb_strlen($newData, $encoding) * mb_strwidth($newData, $encoding);
                Write a Reply...