The perfect solution
This really needs a positive variable width lookbehind . The regular expression will look like this:
~(?<=begin.*)\d+(?=.*end)~s
However, at the time of this writing, the PHP regular expression expression does not support this function. Only fixed width lookbehind is supported. (Taste. Net) though).
Bypass
To achieve our goal, we can use preg_replace_callback with the following regex:
~(?<token>begin|end)|(?<number>\d+)|.*?~s
Code example
function extract_number($input) { function matchNumbers($match) { static $in_region = false; switch ($match['token']) { case 'begin': $in_region=true; break; case 'end': $in_region=false; break; } if ($in_region && isset($match['number'])) { return $match['number'].','; } else { return ''; } } $ret=preg_replace_callback('~(?<token>begin|end)|(?<number>\d+)|.*?~s', 'matchNumbers', $input); return array_filter(explode(',',$ret)); } echo '<pre>'; echo var_dump(extract_number($str)); echo '</pre>';
Output (with an example of OP)
array(3) { [0]=> string(3) "899" [1]=> string(2) "50" }
Stephan
source share