preg_match help

Hi all, i have been trying to use preg_match on a salary that i want to check but i cant seem to get it working.
The format i want is like this e.g. 12,000 the , being compulsory,

Any help would be great thanks

Ian

Ian, I am not certain I understand exactly what you wish to match and not match, but see if this applies:

If you just want to check for any number with commas inserted as commonly seen in the US: /1-9(?:,[0-9]{3})*/

This will not match any preceding characters (such as “$”) or any decimal component. Both of these would be easy to add

This would match: 12,000 AND 3,000 AND 12 AND 123,492,092
This would not match: 12000 OR 03,000

In the above example, for 12000 the matched portion would be 120 and for 03,000 it would be 3,000

Are you sure that regex is actually necessary for your application. (There are cases where it would make sense, but there may be a better solution depending on the purpose)

Thanks for your reply, I think regex is ideal for what I need, basically I have two form fields where a client can input a salary from and a salary to e.g. 12,000 to 18,000, I want all the salary to be formatted the same way, what I mean by that is that they all contain one , between two sets of numbers.

So far I can get it to remove any $ or £ signs but I cant make it compulsory that there is 1 , between two sets of numbers in the salary.

12,000 ok
12000 Not ok

Hope that makes more sence

Thanks

The regex I posted should do what you are looking for. Consider the following:
[php]$numbers = array(‘12,000’,‘12000’);

foreach($numbers as $num)
{
$pattern = ‘/1-9(?:,[0-9]{3})*/’;
preg_match($pattern, $num, $matches);
if($matches[0] == $num) echo $matches[0];
else echo “$num is not a correct format!”;
echo ‘
’;
}[/php]

This will output the following:12,000 12000 is not a correct format!

I believe this is what you are looking for (in theory, not the actual response text).

Is there a specific reason that you are requiring the comma? You could allow the user to input the number with or without the comma and then format it using number_format:
[php]$numbers = array(“12,000”,“12000”);
foreach($numbers as $num)
{
echo number_format(str_replace(’,’, ‘’, $num),0);
echo ‘
’;
}[/php]
Will give the following results:12,000 12,000

In addition to providing more flexibility, there are other advantages to this approach as well. Using str_replace should be faster than preg_match. This will be insignificant if you are dealing with one number at a time, but it can add up. Also, if you are storing the values in a database, you can use a numeric type instead of something like a char or varchar. This will result in a smaller size and in many cases, substantially faster queries.

One final note regarding number_format for display is that you can freely change the output style to match different needs or regional formatting differences. For example: 12,000.00 for an accountant or 12.000 for those in most countries outside of the US and UK.

Let me know if this isn’t working for you. If it isn’t, please provide the number that isn’t being handled correctly and how it should be handled. We should be able to get you taken care of pretty easily.

Thanks alot for the detailed reply, i will try it tommorow and get back to you.

Thanks again

ian

Hi, just an update, it worked perfectly thanks alot. I haven’t used number_format before so that was why I opted for the regex.

Cheers Ian

Glad to hear it Ian!

Best,

Jay

Sponsor our Newsletter | Privacy Policy | Terms of Service