Let's say I have the string "Bob is my friend. Bob likes to eat apples. Goodbye!".
I give a function that string, and the two strings "Bob" and "apples". It returns "Bob likes to eat apples". I seem to have trouble making this function.
If I wanted it to return "Bob is my friend. Bob likes to eat apples" that would be much simpler, but I am trying to get the smallest amount of characters between the two strings.
I tried doing this, but seem to either get stuck on an infinite loop or return the entire inputted string.
function between($strIn, $str1, $str2){
$pos2 = stripos($strIn, $str2);
$pos = 1;
$strStore = "";
while($pos>0 && $pos<$pos2){
$pos = stripos($strIn, $str1);
$strStore = substr($strin, $pos, $pos2-$pos);
$strIn = substr_replace($srtIn, $strtemp, "");
}
return $strStore;
}
What it does it finds the first occurrence of "apples" ($str1) and then keeps finding and deleting every occurrence of "Bob" ($str2) until the position of "Bob" surpasses the position of "apples".
As you can tell, it doesn't work. Could anyone help me out, or tell me what I am doing wrong?