Jump to content

Need help with preg_match_all matching numbers with decimals


alien73

Recommended Posts

Hi there,

 

How would I change the reg expressions to allow any number format like

 

 

  1.23

10.23

100.23

1000.23 

 

Right now I have the code to match a ( - or +)  then a number, digit then 2 numbers.

 

preg_match_all("/[-|+]\s\d{1}\.\d{2}/", $sym_price, $matches); 

 

 

Thanks in advance

 

Thanks, but it doesn't work perhaps it's because it's not checking for a space after the - or + sign? Not sure? Sorry for got to mention that.. oops

 

I have a text box and I want preg_match_all to check for the - or + sign then a space then the price.

 

I'm setting up a down and dirty product options with prices feature

 

- 5.00 + 25.00 +105.00 etc...

You mean multiple numbers before the decimal?

preg_match_all("/[-|+]\s\d+\.\d{2}/", $sym_price, $matches);

 

 

correct!

 

Heh... are you sure? :P

 

<?php
$sym_price = 'My name is Daniel| 5.00 hello world ';

if (preg_match_all("/[-|+]\s\d+\.\d{2}/", $sym_price, $matches)) {
    echo 'It matches';
}

[abc]? will work just fine :)

 

See:

daniel@daniel-laptop:~$ cat test.php
<?php
$pattern = '/^[abc]?$/';
var_dump(preg_match($pattern, 'a'));
var_dump(preg_match($pattern, ''));
var_dump(preg_match($pattern, 'hello'));
?>
daniel@daniel-laptop:~$ php test.php
int(1)
int(1)
int(0)

 

Quantifiers just operate on the previous subpattern and a character class qualifies as a subpattern.

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.