user969068
user969068

Reputation: 2943

PHP equivalent for python string slice

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;  
}

Upvotes: 1

Views: 1065

Answers (3)

Ricardo C&#225;rdenes
Ricardo C&#225;rdenes

Reputation: 9172

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.

Upvotes: 2

Razvan
Razvan

Reputation: 2596

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);

Upvotes: 0

markcial
markcial

Reputation: 9323

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

Upvotes: 0

Related Questions