$str = 'before-str-after'; if (preg_match('/before-(.*?)-after/', $str, $match) == 1) { echo $match[1]; }