Web Design and Web Development Forum

  1. #1
    Join Date
    Oct 2006
    Location
    Fairfield, CA
    Age
    21
    Posts
    206
    Rep Power
    6
  2. aliasxneo will become famous soon enough
  3. String bit length?

    Does anyone know how to get the bit length of a string in PHP? Thanks.

    - Josh
    Reply With Quote Reply With Quote
  4. #2
    Join Date
    Dec 2006
    Location
    University of York, UK
    Age
    22
    Posts
    1,837
    Rep Power
    7
  5. Mike Tomasello is on a distinguished road
  6. Re: String bit length?

    Quote aliasxneo originally posted: View Post
    Does anyone know how to get the bit length of a string in PHP? Thanks.

    - Josh
    I must admit I have no idea what you're getting at, but might strlen work?

    Mike
    Reply With Quote Reply With Quote
  7. #3
    Join Date
    Oct 2006
    Location
    Fairfield, CA
    Age
    21
    Posts
    206
    Rep Power
    6
  8. aliasxneo will become famous soon enough
  9. Re: String bit length?

    I'm trying to determine the amount of space a string takes up in bytes. I know how to do it in MySQL:

    Code:
    SELECT SUM(BIT_LENGTH(blobfieldname)) FROM DATA WHERE user_id=1
    That will give me the number of bytes for "blobfieldname" where "user_id" is equal to 1.

    Oh, and strlen wouldn't work because a character does not exactly represent a byte.
    Reply With Quote Reply With Quote
  10. #4
    Join Date
    Dec 2006
    Location
    University of York, UK
    Age
    22
    Posts
    1,837
    Rep Power
    7
  11. Mike Tomasello is on a distinguished road
  12. Re: String bit length?

    Quote aliasxneo originally posted: View Post
    I'm trying to determine the amount of space a string takes up in bytes. I know how to do it in MySQL:

    Code:
    SELECT SUM(BIT_LENGTH(blobfieldname)) FROM DATA WHERE user_id=1
    That will give me the number of bytes for "blobfieldname" where "user_id" is equal to 1.

    Oh, and strlen wouldn't work because a character does not exactly represent a byte.
    Okay

    How about this:

    Quote PHP Manual originally posted:
    mb_strwidth

    (PHP 4 >= 4.0.6, PHP 5)
    mb_strwidth -- Return width of string
    Description

    int mb_strwidth ( string str [, string encoding] )

    mb_strwidth() returns width of string str.
    Multi-byte character usually twice of width compare to single byte character.

    Table 1. Characters width
    Chars..............Width
    U+0000 - U+0019....0
    U+0020 - U+1FFF....1
    U+2000 - U+FF60....2
    U+FF61 - U+FF9F....1
    U+FFA0 -
    ...........2

    encoding is character encoding. If it is omitted, internal encoding is used.
    Sorry if I'm being useless :D

    Mike
    Reply With Quote Reply With Quote
  13. #5
    Join Date
    Oct 2006
    Location
    Fairfield, CA
    Age
    21
    Posts
    206
    Rep Power
    6
  14. aliasxneo will become famous soon enough
  15. Re: String bit length?

    Quote Mike Tomasello originally posted: View Post
    Okay

    How about this:

    Sorry if I'm being useless :D

    Mike
    It's okay :p But yea don't think that's it either, I don't even have the function defined:

    Fatal error: Call to undefined function mb_strwidth() in C:\webpages\test.php on line 4


    But after reading the PHP documentation I think that's just encoding.
    Reply With Quote Reply With Quote
  16. #6
    Join Date
    Dec 2006
    Location
    University of York, UK
    Age
    22
    Posts
    1,837
    Rep Power
    7
  17. Mike Tomasello is on a distinguished road
  18. Re: String bit length?

    Quote aliasxneo originally posted: View Post
    It's okay :p But yea don't think that's it either, I don't even have the function defined:



    [/b]But after reading the PHP documentation I think that's just encoding.
    If you know the 'width' of a character, then can't you determine its size in bytes from its length? For example, an ASCII character is 8-bit, so if you have a string "hello" in ASCII, is it not 40 bits wide? Maybe there are subtleties of character encoding that I have no idea about.

    Mike
    Reply With Quote Reply With Quote
  19. #7
    Join Date
    Oct 2006
    Location
    Fairfield, CA
    Age
    21
    Posts
    206
    Rep Power
    6
  20. aliasxneo will become famous soon enough
  21. Re: String bit length?

    Quote Mike Tomasello originally posted: View Post
    If you know the 'width' of a character, then can't you determine its size in bytes from its length? For example, an ASCII character is 8-bit, so if you have a string "hello" in ASCII, is it not 40 bits wide? Maybe there are subtleties of character encoding that I have no idea about.

    Mike
    I'm not really sure on that, which is why I'm looking for a function to handle that for me :p
    Reply With Quote Reply With Quote

Similar Threads

  1. Weird Packet Loss
    By Mau in forum Computer Corner
    Replies: 12
    Last Post: 07-20-2006, 12:18 PM
  2. java to oracle connection
    By harsh in forum Java Programming
    Replies: 3
    Last Post: 07-03-2006, 08:55 AM
  3. Problem with a Kill Tracker
    By Vouksh in forum Visual Basic Programming
    Replies: 1
    Last Post: 04-24-2006, 06:40 PM
  4. Awesome/funny/interesting scripts thread
    By neoeno in forum General Programming
    Replies: 10
    Last Post: 04-17-2006, 03:02 PM
  5. Black jack help plz!
    By kr777 in forum Java Programming
    Replies: 13
    Last Post: 04-17-2006, 10:15 AM