我必须将两个 Python 函数翻译成 PHP。第一个是:
def listspaces(string):
return [i -1 for i in range(len(string)) if string.startswith(' ', i-1)]
我假设这将检查提供的字符串中的空间并在找到第一次出现空间时返回 True,这是正确的吗?
这是什么i-1
?是-1
吗?
在 PHP 中,我们使用[]
数组。这里我们[]
使用返回,这个函数会返回 true 还是 false 或空格位置数组?
第二个功能是
def trimcopy(copy, spaces, length=350):
try:
if len(copy) < length:
return copy
else:
loc = 0
for space in spaces:
if space < length:
loc = space
else:
return copy[:loc]
except :
return None
空间中的空间是什么:这里和这是什么 return copy[:loc]