1

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
1
  • Do you know what kind of object file['k'] is? Commented Apr 28, 2014 at 16:01

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

1 Comment

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.
1

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.