Jump to content

Grabbing a substring between two strings.


Liquos

Recommended Posts

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?

Link to comment
Share on other sites

Try this out, it can probably be done another way, but this works for me :)

 

<?PHP

function between($input, $start, $end){
  $length = strlen($input);
  $cutStr = explode($start,$input);

  foreach($cutStr AS $string) {
    if(strstr($string,$end)) {
      $cutStr  = explode($end,$string);
      $tString = $start.$cutStr[0].$end;

      if(strlen($tString) < $length) {
        $length = strlen($tString);
        $output = $tString;
      }
    }
  }

  return $output;
}

  $string = between('Bob and his friends apples. Bob likes to eat apples. Goodbye!','Bob','apples');

  echo $string;
?>

 

Hope this helps, tell me how it goes.

 

Regards, PaulRyan.

Link to comment
Share on other sites

Try this out, it can probably be done another way, but this works for me :)

 

<?PHP

function between($input, $start, $end){
  $length = strlen($input);
  $cutStr = explode($start,$input);

  foreach($cutStr AS $string) {
    if(strstr($string,$end)) {
      $cutStr  = explode($end,$string);
      $tString = $start.$cutStr[0].$end;

      if(strlen($tString) < $length) {
        $length = strlen($tString);
        $output = $tString;
      }
    }
  }

  return $output;
}

  $string = between('Bob and his friends apples. Bob likes to eat apples. Goodbye!','Bob','apples');

  echo $string;
?>

 

Hope this helps, tell me how it goes.

 

Regards, PaulRyan.

 

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.