dongxuan1660 2014-02-15 11:54 采纳率: 100%
浏览 94
已采纳

PHP相当于python字符串切片

I am not familiar with python and need to convert a python script to php. I have done most of but I could not get these string to be converted to php

temp=int(encodedurl[strlen-4:strlen],10)
encodedurl=encodedurl[0:strlen-4]

Here is python script:

def decodeurl(encodedurl):
    tempp9 =""
    tempp4="1071045098811121041051095255102103119"
    strlen = len(encodedurl)
    temp5=int(encodedurl[strlen-4:strlen],10)
    encodedurl=encodedurl[0:strlen-4]
    strlen = len(encodedurl)
    temp6=""
    temp7=0
    temp8=0
    while temp8 < strlen:
        temp7=temp7+2
        temp9=encodedurl[temp8:temp8+4]
        temp9i=int(temp9,16)
        partlen = ((temp8 / 4) % len(tempp4))
        partint=int(tempp4[partlen:partlen+1])
        temp9i=((((temp9i - temp5) - partint) - (temp7 * temp7)) -16)/3
        temp9=chr(temp9i)
        temp6=temp6+temp9
        temp8=temp8+4
    return temp6

And here is my php conversion:

function decode($encodedurl)
{
    $tempp9 ="";
    $tempp4="1071045098811121041051095255102103119";
    $strlen = strlen($encodedurl);
    $temp5=intval($encodedurl[$strlen-4],10);
    $encodedurl=$encodedurl[0:$strlen-4];
    echo $encodedurl; die();
    $strlen = strlen($encodedurl);
    $temp6="";
    $temp7=0;
    $temp8=0;
    while ($temp8 < $strlen)
      $temp7=$temp7+2;
        $temp9=$encodedurl[$temp8:$temp8+4];
        $temp9i=intval($temp9,16);
        $partlen = (($temp8 / 4) % strlen($tempp4));
        $partint=intval($tempp4[$partlen:$partlen+1]);
        $temp9i=(((($temp9i - $temp5) - $partint) - ($temp7 * $temp7)) -16)/3;
        $temp9=chr($temp9i);
        $temp6=$temp6+$temp9;
        $temp8=$temp8+4;
    return $temp6;  
}

Kindly can someone tell me what is equivalent of that in php?

Update php function:

function decode($encodedurl)
{
    $tempp9 ="";
    $tempp4="1071045098811121041051095255102103119";
    $strlen = strlen($encodedurl);
    $temp5=intval(substr($encodedurl, -4));
    $encodedurl=substr($encodedurl, 0, -4);

    $strlen = strlen($encodedurl);
    $temp6="";
    $temp7=0;
    $temp8=0;
    while ($temp8 < $strlen){
        $temp7=$temp7+2;
        $temp9=substr($encodedurl, $temp8, 4);
        $temp9i=intval($temp9,16);
        $partlen = (($temp8 / 4) % strlen($tempp4));
        $partint=substr($tempp4,$partlen,1);
        $temp9i=(((($temp9i - $temp5) - $partint) - ($temp7 * $temp7)) -16)/3;
        $temp9=chr($temp9i);
        $temp6=$temp6.$temp9;
        $temp8=$temp8+4;
        }
        echo $temp6; die();
    return $temp6;  
}
  • 写回答

3条回答 默认 最新

  • dqpwdai095465 2014-02-15 12:00
    关注

    Slicing works on any sequence on Python, not only on strings, but when applied to strings, this:

    s[a:b]
    

    is the same as:

    substr(s, a, b-a)
    

    for 0 <= a <= b

    Edit: I mention the condition on indices being equal or greater than 0 to keep the code exactly the same... Now, there are a few things you may improve on this, because negative indices apply to both Python and PHP.

    Python encodedurl[strlen-4:strlen] is the same as encodedurl[-4:], which would translate as PHP substr($encodeurl, -4)

    Python encodedurl[0:strlen-4] is the same as encodeurl[:-4], which would translate as substr($encodeurl, 0, -4)

    Etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services