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

Link to comment
Share on other sites

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";
}

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.