Re: Re: Help with a regular expression for 0,1 or 2 decimal places

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Phillip Oertel a écrit :
> hi,
>
> i'm not a regex guru myself, but the following regex should work - tabke
> a look at my regex test setup ...
>
> <?php
>
> $test = array(
>     "1",
>     "1.",
>     "1.2",
>     "1.23",
>     "1.234",
>
>     "1234",
>     "1234.",
>     "1234.5",
>     "1234.56",
>     "1234.567"
> );
>
>
> // if there's a dot, we want at least one number after it.
> $regex = '/^\d+(.\d{1,2})?$/';

It would be better to escape the point, cause the point greps for everything.
$regex = '/^\d+(\.\d{1,2})?$/';
Without escaping the point, it could match for '1234:567' or something else.


> // test all or numbers ...
> foreach ($test as $number) {
>    if (preg_match($regex, $number)) {
>       echo "ok ";
>    }  else {
>       echo "no ";
>    }
>    echo $number . "<br />\n";
> }
>
> ?>
>
> i don't think there's a difference between \d and [0-9], but again, i am
> no expert ...
>
> regards,
> phillip

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux