Jump to content

ereg and preg_match give a different result


AP81

Recommended Posts

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)
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.