Convert a Perl code to PHP

前端 未结 8 1503
粉色の甜心
粉色の甜心 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条回答
  •  梦毁少年i
    2021-01-29 09:50

    It looks like you're having trouble with the fact that in Perl, if a function call is placed in the middle of a parameter list, and the called function returns a list, the items in that list are "flattened" to produce multiple arguments to the outer function; PHP doesn't do anything similar, and that's where you're getting your argument mismatch (the split should be producing four arguments to pack, but PHP only sees one -- an array value).

    Fortunately the way around this is pretty easy, because there are builtin functions that will replicate what you need without any gymnastics. Try:

    echo pack("SSA12ANL",
    '25',
    '00001',
    '2u7wx6fd94fd',
    'f',
    ip2long('10.2.1.1'),
    '1278761963');
    

    or if that somehow fails:

    echo pack("SSA12Aa4L",
    '25',
    '00001',
    '2u7wx6fd94fd',
    'f',
    inet_pton('10.2.1.1'),
    '1278761963');
    

提交回复
热议问题