首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP等效于python字符串片

PHP等效于python字符串片
EN

Stack Overflow用户
提问于 2014-02-15 11:54:45
回答 3查看 890关注 0票数 1

我不熟悉python,需要将python脚本转换为php。我已经完成了大部分工作,但无法将这些字符串转换为php。

代码语言:javascript
复制
temp=int(encodedurl[strlen-4:strlen],10)
encodedurl=encodedurl[0:strlen-4]

下面是python脚本:

代码语言:javascript
复制
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转换:

代码语言:javascript
复制
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函数:

代码语言:javascript
复制
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;  
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-02-15 12:00:27

切片工作在Python上的任何序列上,不仅在字符串上,而且在应用于字符串时,如下所示:

代码语言:javascript
复制
s[a:b]

与以下相同:

代码语言:javascript
复制
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)

等。

票数 1
EN

Stack Overflow用户

发布于 2014-02-15 11:59:57

你在这条线路上有个问题

代码语言:javascript
复制
$temp5=intval($encodedurl[$strlen-4],10);
$encodedurl=$encodedurl[0:$strlen-4];

因为php不将字符串视为数组,所以需要使用像底座这样的函数

票数 0
EN

Stack Overflow用户

发布于 2014-02-15 12:02:06

Python的串切片非常类似于底座函数。

那么,您的Python代码:

代码语言:javascript
复制
temp=int(encodedurl[strlen-4:strlen],10)
encodedurl=encodedurl[0:strlen-4]

翻译成以下PHP代码:

代码语言:javascript
复制
$temp=intval(substr($encodedurl, -4));
$encodedurl=substr($encodedurl, 0, -4);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21797181

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档