Howto read bytes from string

Discussion in 'PHP' started by NickD, Aug 22, 2007.

  1. #1
    For example:
    The string is: PO3nX+Dk+pjwOtTaFJOgoYYFqmOMO38pKA5NUkgK+gVSi+/6gVor1JfbnUvpjG9YevClBsi
    KjOgp9HOY6tF7Pw==

    I want to read 0x20 from the beginning of it, And 0x20 from the end of it. Thanks for any help!
     
    NickD, Aug 22, 2007 IP
  2. DeViAnThans3

    DeViAnThans3 Peon

    Messages:
    785
    Likes Received:
    83
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Is this what you're looking for:

    
    <?php
    $string = "PO3nX+Dk+pjwOtTaFJOgoYYFqmOMO38pKA5NUkgK+gVSi+/6gVor1JfbnUvpjG9YevClBsi
    KjOgp9HOY6tF7Pw==";
    $string_first_part = substr($string, 0, 20);
    $strlength = strlen($string);
    $strstart = $strlength - 20;
    $string_last_part = substr($string, $strstart, 20);
    
    // $string_first_part and $string_last_part contain first and last 20 characters of the string $string.
    // do with it what you want below.
    ?>
    PHP:
    hans
     
    DeViAnThans3, Aug 23, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    This part could be simplified by:
    
    $string_last_part = substr($string, -20);
    
    PHP:
     
    nico_swd, Aug 23, 2007 IP
    DeViAnThans3 likes this.
  4. codesome

    codesome Peon

    Messages:
    98
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    0x20 = 32, not 20
    ...
    $string_first_part = substr($string, 0, 0x20);
    $string_last_part = substr($string, -0x20);
    ...
    PHP:
     
    codesome, Aug 23, 2007 IP
  5. DeViAnThans3

    DeViAnThans3 Peon

    Messages:
    785
    Likes Received:
    83
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks both of you :D
    I'm learning here .. lol
    I thought 0x20 just was to say 0 to 20, but had it wrong then :)
     
    DeViAnThans3, Aug 23, 2007 IP
  6. codesome

    codesome Peon

    Messages:
    98
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #6
    BTW, 0xdigit is hexadecimal of digit.
     
    codesome, Aug 23, 2007 IP
  7. NickD

    NickD Well-Known Member

    Messages:
    262
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    130
    #7
    Thanks guys.
     
    NickD, Aug 23, 2007 IP