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

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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongzj2015 2014-02-15 11:59

    you have a problem on this lines

    $temp5=intval($encodedurl[$strlen-4],10);
    $encodedurl=$encodedurl[0:$strlen-4];
    

    its because php doesn't treats strings as arrays, you need to use functions like substr

    评论
    解决 无用
    打赏 举报
  • dongxia9519 2014-02-15 12:02

    Python's string slicing is very similar to PHP's substr function.

    So, your Python code:

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

    Translates to the following PHP code:

    $temp=intval(substr($encodedurl, -4));
    $encodedurl=substr($encodedurl, 0, -4);
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题