3v4l.org

run code in 200+ php & hhvm versions
<?php function find_between($string, $start, $end, $trim = true, $greedy = false) { $pattern = '/' . preg_quote($start) . '(.*'; if (!$greedy) $pattern .= '?'; $pattern .= ')' . preg_quote($end) . '/'; preg_match($pattern, $string, $matches); $string = $matches[0]; if ($trim) { $string = substr($string, strlen($start)); $string = substr($string, 0, -strlen($start) + 1); } return $string; } function find_between2($string, $start, $end, $trim = true, $greedy = false) { $stringOut = ''; $pattern = '/'.preg_quote($start).'(.*'; if (!$greedy) $pattern .= '?'; $pattern .= ')'.preg_quote($end).'/'; preg_match($pattern, $string, $matches); if (count($matches)>1) { if ($trim) { $stringOut = $matches[1]; } else { $stringOut = $matches[0]; } } return ($stringOut==='')?false:$stringOut; } $string = 'The quick brown fox jumps over the lazy (not jumping) dog'; $start = 'quick'; $end = 'jump'; var_dump(find_between($string, $start, $end)); var_dump(find_between2($string, $start, $end));
Output for 4.3.0 - 5.6.30, hhvm-3.10.1 - 3.22.0, 7.0.0 - 7.2.8
string(11) " brown fox " string(11) " brown fox "