I am trying to convert this php function
function strpos_r($haystack, $needle)
{
if(strlen($needle) > strlen($haystack))
trigger_error(sprintf("%s: length of argument 2 must be <= argument 1", __FUNCTION__), E_USER_WARNING);
$seeks = array();
while($seek = strrpos($haystack, $needle))
{
array_push($seeks, $seek);
$haystack = substr($haystack, 0, $seek);
}
return $seeks;
}
I have written this python function, but is not working as expected.
def strposR(haystack, needle):
if strlen(needle) > strlen(haystack):
sys.stderr.write("length of argument 2 must be <= argument 1")
seeks = []
seek = 0
while seek == haystack.rfind(needle):
seeks.append(seek)
haystack = haystack[0:seek]
return seeks
def strlen(x):
return len(x)
What I am doing wrong? Any pointers will be much appreciated.
This works for me:
Also, functions should not really handle input errors for you. You usually just
return Falseor let the function throw an execution error.