2014-02-15 30 views
0

我不熟悉python,需要將python腳本轉換爲php。我已經做了大部分,但我不能讓這些字符串轉換到PHPpython字符串切片的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; 
} 

回答

1

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

等等

+0

謝謝你的解釋。我已經更新了我所瞭解的代碼,但結果並不是我期待的。 – user969068

+0

你在做什麼? –

+0

用示例輸入和預期輸出來更新您的問題也是有用的 –

0

你對這個線

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

它,因爲PHP不把字符串作爲數組,你需要使用的功能,如substr

有問題
0

Python的string slicing很相似到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);