Luis Armando
Luis Armando

Reputation: 454

Line in Python to PHP

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?

Upvotes: 1

Views: 91

Answers (2)

vaultah
vaultah

Reputation: 46533

Given file['k'] is a string, here's the equivalent in PHP:

$key = substr(file['k'], strpos(file['k'], ':') + 1);

Upvotes: 2

Rob Watts
Rob Watts

Reputation: 7146

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']

Upvotes: 1

Related Questions