Liquos Posted April 23, 2011 Share Posted April 23, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/234491-grabbing-a-substring-between-two-strings/ Share on other sites More sharing options...
wildteen88 Posted April 23, 2011 Share Posted April 23, 2011 Not sure what is wrong with your code but you could use regex for this. Like this pattern. Quote Link to comment https://forums.phpfreaks.com/topic/234491-grabbing-a-substring-between-two-strings/#findComment-1205214 Share on other sites More sharing options...
PaulRyan Posted April 23, 2011 Share Posted April 23, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/234491-grabbing-a-substring-between-two-strings/#findComment-1205289 Share on other sites More sharing options...
Liquos Posted April 23, 2011 Author Share Posted April 23, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/234491-grabbing-a-substring-between-two-strings/#findComment-1205376 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.