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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

[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.

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.