1

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 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for explanation. I have updated code from what I understood but result is not what I was expecting.
What are you getting?
It would also be useful to update your question with an example input and the expected output
Thank you, I actually dont have input. I am trying to decode string from database which is being used by python script. I will post below output, can u please check if I am setting correct translation for $temp9 and $partint, because $partlen is never used in my updated function...here output string... tmurl*401717b4c50d526fb6a6cc19150c106e40b6345af53338cb91ff51eabea441cfa94cff5fc856d8d476d04f8088cf433316ca4955c2f7a7c6adba07d3a008280cf514b31cd86cacff571b657f0c67f8727991c73db2b423f5dc5ee5f14e815e38
I figured it out and updated function. Many thanks again
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.