My question is quite simple, I have a line written in Python and I want to translate to PHP. I would like to know what this code means, can anyone help me?
key = file['k'][file['k'].index(':') + 1:]
PHP writing, $file looks like:
stdClass Object ( [h] => EtJ2BShT [p] => RgwTgawT [u] => Wh2yQMRtPnM [t] => 0 [a] => 7t-LChJ3ipLxISzHTJ3P8ctqt5YYZoXXJ_3uQwc65sU [k] => Wh2yQMRtPnM:dmcr_plTOPAS75PLnr3qlXZnK_6ZUzjwEu-Ty5696pU [s] => 85 [ts] => 1398630915 )
So, if I understood your comments, $key = dmcr_plTOPAS75PLnr3qlXZnK_6ZUzjwEu-Ty5696pU is the correct answer. Ok?
vaultah
46.9k13 gold badges120 silver badges145 bronze badges
asked Apr 28, 2014 at 15:59
Luis Armando
4541 gold badge7 silver badges18 bronze badges
2 Answers 2
Given file['k'] is a string, here's the equivalent in PHP:
$key = substr(file['k'], strpos(file['k'], ':') + 1);
answered Apr 28, 2014 at 16:06
vaultah
46.9k13 gold badges120 silver badges145 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Rob Watts
Your line would not have the same behavior -
lists do not have a split method, and in a string it would grab everything after the last colon and be transformed into a list. OP's code grabs everything after the first colon and retains it as a list or string.This line will take everything from file['k'] after the first colon. For example:
>>> teststr = 'hello:world'
>>> teststr[teststr.index(':') + 1:]
'world'
Breaking it up into its parts:
>>> teststr.index(':')
5
>>> teststr[5]
':'
>>> teststr[5:]
':world'
>>> teststr[6:]
'world'
Here I'm using a string, but this will behave in the same way if file['k'] is a list:
>>> testlist = ['h', 'e', 'l', 'l', 'o', ':', 'w', 'o', 'r', 'l', 'd']
>>> testlist[testlist.index(':')+1:]
['w', 'o', 'r', 'l', 'd']
answered Apr 28, 2014 at 16:03
Rob Watts
7,1663 gold badges42 silver badges60 bronze badges
Comments
default
file['k']is?