TexasMd91 Posted September 22, 2007 Share Posted September 22, 2007 Lets say I have a string with the value "Robots > Bunnies" Is there any way that I could get everything between "robots" and "bunnies" and put it into its own string Example: <?php function getInBetween($string, $before, $after, $newstring) { //function to get whats between $before and $after from $string and put it in $newstring } getInBetween("My name is Mark.","My name is ", ".", $name); ?> SO it would get what is between "My name is " and "." so it would get my name. Ofcourse this is just an example and not what I am using it for Quote Link to comment https://forums.phpfreaks.com/topic/70258-getting-everything-between-2-words-in-a-string/ Share on other sites More sharing options...
rarebit Posted September 22, 2007 Share Posted September 22, 2007 It's vulgar, but... function get_mid($s, $start, $end) { return trim(substr($s, strlen($start), -(strlen($end)))); } $ss = "test name test"; $n = get_mid($ss, "test", "test"); echo "name: ".$n."<br>"; You could count the strings first and pass the length, esp if repeated! Quote Link to comment https://forums.phpfreaks.com/topic/70258-getting-everything-between-2-words-in-a-string/#findComment-352869 Share on other sites More sharing options...
Psycho Posted September 22, 2007 Share Posted September 22, 2007 <?php $subject = "My Name is Bob Hope."; $pattern = '/My Name is(.*?)\./'; preg_match($pattern, $subject, $matches); print_r($matches); ?> Output: Array ( [0] => My Name is Bob Hope. [1] => Bob Hope ) Quote Link to comment https://forums.phpfreaks.com/topic/70258-getting-everything-between-2-words-in-a-string/#findComment-352872 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.