繁体   English   中英

PHP等价于python字符串切片

[英]PHP equivalent for python string slice

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

切片可在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)

等等。

你在这条线上有问题

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

这是因为php不会将字符串视为数组,因此您需要使用诸如substr之类的函数

Python的字符串切片与PHP的substr函数非常相似。

因此,您的Python代码:

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

转换为以下PHP代码:

$temp=intval(substr($encodedurl, -4));
$encodedurl=substr($encodedurl, 0, -4);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM