Jump to content

ereg() - error trying to match a positive decimal with 0, 1 or 2 decimal places


Geehog

Recommended Posts

I am trying to match a number that is greater than or equal to 0 with 0, 1 or 2 decimal places.

 

I got the following regular expression from http://regexplib.com/REDetails.aspx?regexp_id=543 :

 

^\d+(?:\.\d{2,2})?$

 

So i create this test php file and execute:

 

<?php

$value = "1.05";

if(!ereg("^\d+(?:\.\d{2,2})?$",$value)) 
{
echo "no match";
}
else
{
echo "match";
}

?>

 

It should give me a match based on regular expression but it gives me the following output:

 

Warning: ereg() [function.ereg]: REG_BADRPT in C:\wamp\www\test2.php on line 5

no match

 

 

Can anyone advise what I am doing wrong?

 

Please note that I have searched many places on the web and cannot determine the problem.

 

Thanks

 

Geehog

Rather than investing too much time fixing the error can I suggest you switch to using preg_match since ereg is a deprecated function. It would roughly look something like this...

 

$value = "1.05";

if(!preg_match('#^[0-9]+(?:\.[0-9]{1,2})?$#', $value)) {
   echo "no match";
} else {
   echo "match";
}

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.