Jump to content

Liquos

New Members
  • Posts

    2
  • Joined

  • Last visited

    Never

Everything posted by Liquos

  1. Thanks! This worked perfectly! Now I'm curious as to what I did wrong in my code... Anyways, while I'm here I might as well ask another somewhat-related question. Let's say I wanted to find the nearest substring to another given substring. What I mean is if I give a function "Bob and his friends apples. Bob likes to eat apples. Goodbye!" "apples" "Bob" It would return the position of the nearest "Bob" to the first occurrence of "Apples". The position of Bob and his friends apples. Bob likes to eat apples. Goodbye! otherwise written as 29. I had an idea on how to do the first function, but I'm not sure how I might go about doing this one.
  2. 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?
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.