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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.