Re: [PHP] Removing commas from number

2007-05-07 Thread Richard Lynch


On Sun, May 6, 2007 10:33 am, Todd Cary wrote:
 Thanks to the suggestions, I use number_format($my_number, 2) to
 format the
 number in an edit field.  Now I need to reenter it into MySQL.  How
 should I use
 preg_replace to remove the commas?

 This removes the commas *and* the decimal point:

 preg_replace('/\D/', '', $str)

 In reviewing patterns, I cannot find the purpose of the / character
 in the above.

//to JUST replace the comma:
$str = str_replace(',', $str);

The '/' is not part of the pattern, but a delimiter to set pattern
separate from modifiers.
http://php.net/pcre

You could also do something not unlike:

$str = preg_replace(|[^0-9\\.-]|', '', $str);

Here I chose '|' for the delimiter instead of '/' because I felt like it.

You can choose different things in different circumstances being easier.

If '/' is actually IN the pattern, it's clunky as a delimiter because
you then have to escape it in the pattern.

By forcing the data to ONLY contain 0-9 . and - you pretty much
drastically increase the odds that any other stray typo won't get
through.

You could also consider testing with 'is_numeric' to rule out goofy
things like:
5.2.2
which is probably not valid input for what you are doing, but does
pass the pattern I've listed...

Or, you could try a more specific pattern:

|-?[0-9]*(\\.[0-9]{2})?|

which I *think* means:

A leading -, maybe
Any number of 0-9 digits
A period *and* 2 more digits, or nothing

This still doesn't guarantee valid input, though, as '' passes this
pattern :-v

The trick is to be as precise as you can about what you accept,
without rejecting anything that is valid.

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

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



[PHP] Removing commas from number

2007-05-06 Thread Todd Cary
Thanks to the suggestions, I use number_format($my_number, 2) to format the 
number in an edit field.  Now I need to reenter it into MySQL.  How should I use 
preg_replace to remove the commas?


This removes the commas *and* the decimal point:

preg_replace('/\D/', '', $str)

In reviewing patterns, I cannot find the purpose of the / character in the 
above.

Many thanks

Todd

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



Re: [PHP] Removing commas from number

2007-05-06 Thread Tijnema !

On 5/6/07, Todd Cary [EMAIL PROTECTED] wrote:

Thanks to the suggestions, I use number_format($my_number, 2) to format the
number in an edit field.  Now I need to reenter it into MySQL.  How should I use
preg_replace to remove the commas?

This removes the commas *and* the decimal point:

preg_replace('/\D/', '', $str)

In reviewing patterns, I cannot find the purpose of the / character in the 
above.

Many thanks

Todd


the / in above is the start and end token. for every preg_* function,
this is used.

You could replace it by any other character as long as it is the same
for start and end.

so above could also be
preg_replace('%\D%','',$str);

I use % nearly allways, because i like it, and it isn't used a lot. /
might be used in paths and can get confusing when \ also used.

Tijnema

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



Re: [PHP] Removing commas from number

2007-05-06 Thread Paul Novitski

At 5/6/2007 08:33 AM, Todd Cary wrote:
Thanks to the suggestions, I use number_format($my_number, 2) to 
format the number in an edit field.  Now I need to reenter it into 
MySQL.  How should I use preg_replace to remove the commas?


This removes the commas *and* the decimal point:

preg_replace('/\D/', '', $str)



This strikes me as such an odd thing to do.  If the MySQL table field 
is defined as float, I don't see the need to remove the decimal point.


If you do need to store the digits without the punctuation, you could 
also multiply by 100 and store it as an integer: intval($my_number * 
100) which seems more efficient than to format as a string and then 
reformat without the punctuation.


If I have two uses for a number -- say, formatted with commas and 
other dressing for display and formatted more severely for SQL -- I'd 
retain the number in a variable as its pure value and convert it for 
each use, rather than converting it for one use and then converting 
that for the next use.  The software's more robust because a glitch 
in a formatting operation isn't going to affect the final 
result.  Also, in many arithmetic circumstances where division is 
involved, the true value of the results are accurate to more than two 
decimal places.  While these might be rounded to the nearest cent for 
display purposes, you'll want to add the true values to get the true 
total.  One common example is a column of percentages that should add to 100%.


Regards,

Paul
__

Paul Novitski
Juniper Webcraft Ltd.
http://juniperwebcraft.com 


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



Re: [PHP] Removing commas from number

2007-05-06 Thread Todd Cary

Paul -

You make a good point.  What is your suggestion for the following sequence:

$number = $row-AMOUNT; // Get the double from MySQL

To display the number in the HTML edit field, I do

$display_number = number_format($number, 2, '.', '');

The user may enter a character that will not be accepted by MySQL, so I do

$mysql_number = preg_replace('/[^0-9^\.]/', '', $display_number);

Thank you for your suggestions

Todd

Paul Novitski wrote:

At 5/6/2007 08:33 AM, Todd Cary wrote:
Thanks to the suggestions, I use number_format($my_number, 2) to 
format the number in an edit field.  Now I need to reenter it into 
MySQL.  How should I use preg_replace to remove the commas?


This removes the commas *and* the decimal point:

preg_replace('/\D/', '', $str)



This strikes me as such an odd thing to do.  If the MySQL table field is 
defined as float, I don't see the need to remove the decimal point.


If you do need to store the digits without the punctuation, you could 
also multiply by 100 and store it as an integer: intval($my_number * 
100) which seems more efficient than to format as a string and then 
reformat without the punctuation.


If I have two uses for a number -- say, formatted with commas and other 
dressing for display and formatted more severely for SQL -- I'd retain 
the number in a variable as its pure value and convert it for each use, 
rather than converting it for one use and then converting that for the 
next use.  The software's more robust because a glitch in a formatting 
operation isn't going to affect the final result.  Also, in many 
arithmetic circumstances where division is involved, the true value of 
the results are accurate to more than two decimal places.  While these 
might be rounded to the nearest cent for display purposes, you'll want 
to add the true values to get the true total.  One common example is a 
column of percentages that should add to 100%.


Regards,

Paul
__

Paul Novitski
Juniper Webcraft Ltd.
http://juniperwebcraft.com


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



Re: [PHP] Removing commas from number

2007-05-06 Thread Paul Novitski

At 5/6/2007 09:39 AM, Todd Cary wrote:

You make a good point.  What is your suggestion for the following sequence:

$number = $row-AMOUNT; // Get the double from MySQL

To display the number in the HTML edit field, I do

$display_number = number_format($number, 2, '.', '');

The user may enter a character that will not be accepted by MySQL, so I do

$mysql_number = preg_replace('/[^0-9^\.]/', '', $display_number);



Ah.  I had earlier assumed that you were supplying the same numeric 
value to two output destinations -- display and SQL.  Instead, you're 
taking a single value from SQL to input and another single value from 
input to SQL.  Even if you understand that these are the same number 
in the context of the application, they could as easily be totally 
separate because the two operations are disconnected from one another:


1) [SQL] -- [transform 1] -- [input]
2) submit form
3) [input] -- [transform 2] -- [SQL]

Transform 1 converts from the pure float value to a formatted string, 
for which number_format() works fine.  (You mentioned that these are 
dollar amounts, but I wouldn't bother including the currency symbol 
in with the input text, rather more like:


Enter price: $[__0.00]

where [___] is the input field.)

Transform 2 converts whatever the user has entered into a valid 
numeric to pass to SQL.  For many applications, I don't think a good 
input validation routine would simply delete any non-numeric 
character from the input.  A user could erroneously type oh for zero, 
el for one, or hit the shift key while typing a digit.  Better, I 
think, to preserve the input if it isn't valid and ask the user to 
reconsider.  Their own correction of their input might be 
significantly different from a simple reduction.


A regular expression pattern to check for valid currency input might be:

[+-($]*\d{1,3}(,\d{3})*(\.\d*){0,1}\){0,1}

[+-($]* zero or more: plus, minus, open-paren, or currency symbol
\d{1,3} one to three: numeric digits
(,\d{3})*   zero or more: comma-digit-digit-digit groups
(\.\d*){0,1}zero or one: decimal point followed by any number of digits
\){0,1} zero or one: close-paren

Any string failing to match this pattern could warrant an error message.

This example is of course dollar-oriented; you may wish to make your 
logic apply equally to foreign currencies.  Note that different 
cultures have very different ways of expressing numbers -- comma for 
the decimal point and period for the thousands separator, different 
numbers of digits between separators, and different characters mixed 
with the digits to indicate scale.


Once you accept the input, then you could delete all the characters 
that aren't digits or period.  Keep that decimal point, it's too 
significant to lose.


Regards,

Paul
__

Paul Novitski
Juniper Webcraft Ltd.
http://juniperwebcraft.com 


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