[F] $strstr(,needle) fails

bug-fixed

#1

$strstr(,needle) returns nothing, whereas I would expect it to return 0 as does $strstr(haystack,needle), given

Help
$strstr(x,y) Finds the first occurrence of string y in string x.

Is this a known bug?


#2

If I remember correctly from the good ole BASIC times the strstr-function (there called "instr") always produced a runtime error if the first string was empty or shorter than the second string. So one (the programmer) had to make sure that the function was let loose on a valid combination.
Years if not decades have passed since ... apparently the function still requires a little common sense.

Which result would you have expected as IMHO checking "nothing" for the presence of a string should not produce the same result as finding that a string is definitely not part of another string? Something that can be expressed by a division by zero?


#3

I am not sure, if it is a bug or a lack of information due to missing documentation.
See yourself ...

$strstr(x,y) liefert die Position an der die Zeichenkette y zum ersten Mal in x auftritt. $strstr('a') ==> nothing, because of Syntax Error? $strstr(,'a') ==> nothing, because of Syntax Error? $strstr('a',) ==> nothing, because of Syntax Error? $strstr(,) ==> nothing, because of Syntax Error? $strstr() ==> nothing, because of Syntax Error? $strstr('','a') ==> 0 $strstr('a','') ==> 0 $strstr('','') ==> 1 $strstr('a''','') ==> 2 $strstr('a','a') ==> 1 $strstr(a,a) ==> 1 $strstr('ba','a') ==> 2 $strstr('a','ba') ==> 0

DD.20110710.2058.CEST


#4

Your mean the function definition requires a little common sense?

It should. If the y is not in x, the result should be zero regardless of the value of x.


#5

Since this cannot be fixed in $strstr fixed without breaking script compatibility, can we please have a $strstr2 in which it is fixed?