2 つの Python 関数を PHP に変換する必要があります。最初のものは次のとおりです。
def listspaces(string):
return [i -1 for i in range(len(string)) if string.startswith(' ', i-1)]
これにより、提供された文字列のスペースがチェックされ、スペースが最初に見つかったときに True が返されると想定していますが、これは正しいですか?
i-1
ここは何ですか?ですか-1
?
PHP では[]
、 for array を使用します。ここ[]
で return を使用します。この関数は true または false またはスペースの位置の配列を返しますか?
2番目の機能は
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
スペース内のスペースのWhats for space: here and whats is this return copy[:loc]