Convert a Perl code to PHP

前端 未结 8 1493
粉色の甜心
粉色の甜心 2021-01-29 08:50

I need to convert the following perl function to php:

pack(\"SSA12AC4L\",
     $id,
     $loc,
     $name,
     \'ar\',
     split(/\\./, $get->getIP),
     t         


        
相关标签:
8条回答
  • 2021-01-29 09:27

    The problem is that the code is giving to pack() (I am referring the the last arguments) a character, an array, and an integer. As the code C wants 4 characters, that is the cause of the error.

    The code should be something like

    $split = preg_split('/\./','10.2.1.1', -1, PREG_SPLIT_NO_EMPTY);
    echo pack("SSA12AC4L",
      '25',
      '00001',
      '2u7wx6fd94fd',
      'f',
      $split[0],
      $split[1],
      $split[2],
      $split[3],
      time()+60*60
    );
    

    Then, there is no reason to use preg_split() in this case, when explode() can be used instead. Regular expressions should be used only when strictly necessary because the regular expression functions are slower, compared to other string functions.

    0 讨论(0)
  • 2021-01-29 09:30

    The problem is that php preg_split is converting it to an array. You need an unsigned char, so use

    $x = intval(implode("", preg_split('/\./','10.2.1.1', -1, PREG_SPLIT_NO_EMPTY)));
    echo pack('SSA12ACL',
         '25',
         '00001',
         '2u7wx6fd94fd',
         'f',
         $x,
         time()+(60*60));
    

    Let know how it goes.

    0 讨论(0)
  • 2021-01-29 09:43

    From the PHP documentation for pack().

    Pack given arguments into binary string according to format.

    The idea for this function was taken from Perl and all formatting codes work the same as in Perl. However, there are some formatting codes that are missing such as Perl's "u" format code.

    Surely it should just work as is? You may need to rename some variables

    0 讨论(0)
  • 2021-01-29 09:45

    My first suggestion is that you carefully read the documentation. This problem has little to do with perl and much to do with understanding what the function expects. My second suggestion is to get in the habit of feeling a little nervous whenever you copy some code. Nervous enough to pay extra attention to the code, the documentation, etc. At the very least, when a client/boss/whoever asks you what that bit of copied code does, you should have a good answer.

    The first parameter to pack() is a format string. This determines how it formats the parameters when it creates the output.

    From the documentation for pack():

    The format string consists of format codes followed by an optional repeater argument. The repeater argument can be either an integer value or * for repeating to the end of the input data. For a, A, h, H the repeat count specifies how many characters of one data argument are taken, for @ it is the absolute position where to put the next data, for everything else the repeat count specifies how many data arguments are consumed and packed into the resulting binary string.

    So, the problem is that your format string isn't appropriate for the arguments you pass to pack(). Now, keep in mind that I have to guess at the appropriate format string for your needs. You have to read the documentation and determine the correct format string.

    The following works just fine:

    echo pack("SSA12ACL",
    '25',
    '00001',
    '2u7wx6fd94fd',
    'f',
    preg_split('/\./','10.2.1.1', -1, PREG_SPLIT_NO_EMPTY),
    '1278761963');
    

    The function preg_split() returns a single array. However, the 'C4' in the original format string expects to take in 4 parameters. Based on my count, the original format string implied 9 parameters, not 6.

    0 讨论(0)
  • 2021-01-29 09:46

    I think that the problem is that preg_split returns an array. So the array is inserted as the first char, the time as the second, and two chars are left.

    I don't know how to fix this problem, but to create a temporary array:

    $ip = explode('.','10.2.1.1');
    

    And then:

    echo pack("SSA12AC4L",
     '25',
     '00001',
     '2u7wx6fd94fd',
     'f',
     $ip[0],
     $ip[1],
     $ip[2]
     $ip[3],
     time()+(60*60));
    
    0 讨论(0)
  • 2021-01-29 09:48

    Sometimes the error statements mean something worth reviewing. Too few arguments may mean there is a need to review each input used in the PHP pack statement aligns with the expected format.

    For instance, did you take into account the 'ar' field used in the perl pack statement? You might be off in the resulting packed data by one field because of that.

    0 讨论(0)
提交回复
热议问题