我不熟悉python,需要将python脚本转换为php。我已经完成了大部分工作,但无法将这些字符串转换为php。
temp=int(encodedurl[strlen-4:strlen],10)
encodedurl=encodedurl[0:strlen-4]下面是python脚本:
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下面是我的php转换:
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;
}请有人告诉我,在php中什么是等价的?
更新php函数:
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;
}发布于 2014-02-15 12:00:27
切片工作在Python上的任何序列上,不仅在字符串上,而且在应用于字符串时,如下所示:
s[a:b]与以下相同:
substr(s, a, b-a)对于0 <= a <= b
编辑:我提到索引等于或大于0的条件,以保持代码完全相同.现在,您可以在这方面做一些改进,因为负索引同时适用于Python和PHP。
Python encodedurl[strlen-4:strlen]与encodedurl[-4:]相同,后者将翻译为PHP substr($encodeurl, -4)。
Python encodedurl[0:strlen-4]与encodeurl[:-4]相同,后者将翻译为substr($encodeurl, 0, -4)。
等。
发布于 2014-02-15 11:59:57
你在这条线路上有个问题
$temp5=intval($encodedurl[$strlen-4],10);
$encodedurl=$encodedurl[0:$strlen-4];因为php不将字符串视为数组,所以需要使用像底座这样的函数
发布于 2014-02-15 12:02:06
Python的串切片非常类似于底座函数。
那么,您的Python代码:
temp=int(encodedurl[strlen-4:strlen],10)
encodedurl=encodedurl[0:strlen-4]翻译成以下PHP代码:
$temp=intval(substr($encodedurl, -4));
$encodedurl=substr($encodedurl, 0, -4);https://stackoverflow.com/questions/21797181
复制相似问题