AP81 Posted August 17, 2011 Share Posted August 17, 2011 Hi, I'm working on some old code (someone else wrote) and have to replace old the old 'ereg' functions to preg_match. As we are moving to PHP 5.3 the ereg functions are deprecated. The funny thing is that these two pieces of code produce different output. One uses ereg and the other preg_match. Any reasons as to why? The code below produces a different size array of matches for $amt3 and $amt4. I can write a work around but am just wondering why the output is different, as all documentation I have read state that ereg and preg_match are almost identical in the result they produce. $amt1 = '$100.00'; $amt2 = '100.00'; $amt3 = '$100'; $amt4 = '100'; function parseFromString($amount) { if (!preg_match('/^([A-Z]{1,3})?(\$)?([0-9]+)((\.)([0-9]+))?$/', $amount, $regs)) { return null; } var_dump($regs); } function parseFromString1($amount) { if (!ereg('^([A-Z]{1,3})?(\$)?([0-9]+)((\.)([0-9]+))?$', $amount, $regs)) { return null; } var_dump($regs); } parseFromString($amt1); parseFromString($amt2); parseFromString($amt3); parseFromString($amt4); echo "\n\n==============================\n\n"; parseFromString1($amt1); parseFromString1($amt2); parseFromString1($amt3); parseFromString1($amt4); Output: array(7) { [0]=> string(7) "$100.00" [1]=> string(0) "" [2]=> string(1) "$" [3]=> string(3) "100" [4]=> string(3) ".00" [5]=> string(1) "." [6]=> string(2) "00" } array(7) { [0]=> string(6) "100.00" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(3) "100" [4]=> string(3) ".00" [5]=> string(1) "." [6]=> string(2) "00" } array(4) { [0]=> string(4) "$100" [1]=> string(0) "" [2]=> string(1) "$" [3]=> string(3) "100" } array(4) { [0]=> string(3) "100" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(3) "100" } ============================== array(7) { [0]=> string(7) "$100.00" [1]=> bool(false) [2]=> string(1) "$" [3]=> string(3) "100" [4]=> string(3) ".00" [5]=> string(1) "." [6]=> string(2) "00" } array(7) { [0]=> string(6) "100.00" [1]=> bool(false) [2]=> bool(false) [3]=> string(3) "100" [4]=> string(3) ".00" [5]=> string(1) "." [6]=> string(2) "00" } array(7) { [0]=> string(4) "$100" [1]=> bool(false) [2]=> string(1) "$" [3]=> string(3) "100" [4]=> bool(false) [5]=> bool(false) [6]=> bool(false) } array(7) { [0]=> string(3) "100" [1]=> bool(false) [2]=> bool(false) [3]=> string(3) "100" [4]=> bool(false) [5]=> bool(false) [6]=> bool(false) } Quote Link to comment https://forums.phpfreaks.com/topic/244974-ereg-and-preg_match-give-a-different-result/ Share on other sites More sharing options...
xyph Posted August 17, 2011 Share Posted August 17, 2011 preg_match() won't create array keys if it doesn't need to. It also returns an empty string instead of FALSE on capturing groups with no return. You can quite easily modify the array to match your old ereg return. Quote Link to comment https://forums.phpfreaks.com/topic/244974-ereg-and-preg_match-give-a-different-result/#findComment-1258386 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.