By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,409 Members | 1,604 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,409 IT Pros & Developers. It's quick & easy.

Regex or str_replace confusion

P: n/a
Hello,

Spent all day yesterday reading about this and I still can't get it.
Perhaps my IQ is not much above room temperature...

My mySQL database is a simple inventory-type database with a "price" field
as an integer. The rest of my script relies on price being an integer so I
really don't want to change the data type.

I have a page with an HTML
form where the user can type in new entries into the database. In my HTML
form, I have the "price" field and I want the user to be able to type the
price any way they want. For instance for an item that costs $10,000 they
could type:

10000
10,000
10000.00
10,000.00
etc.

In the php script that the form posts to, I need to have some error
handling so regardless of how the user types the price on the form, it
will get entered into the database as an integer. I'm looking for
something along the lines of this:

if function_bad_characters($price)
{ $price=function_fix_bad_characters($price); }

and with this the user input in "price" is now reformatted so it gets
entered into the database as an integer.

I have tried all kinds of different regex recipes and str_replace magic
and nothing seems to be working. I'm frustrated enough at this point that
I just want one of you smart people to write it FOR me!!

Any help or pointers in the right direction is appreciated.

Thank you.

--
Will Clifton
wg*******@cox.net

Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 2005-05-04, Will Clifton <wg*******@cox.net> wrote:
My mySQL database is a simple inventory-type database with a "price" field
as an integer. price any way they want. For instance for an item that costs $10,000 they
could type:

10000
10,000
10000.00
10,000.00
etc.
All your examples use the . to indicate the decimals are following.
I know a lot of places/people that use the , for that.

So, this is a locale problem, you probably want to check that out in the
manual.
and with this the user input in "price" is now reformatted so it gets
entered into the database as an integer.
<?php
$money = array('10000', '10,000', '10000.00', '10,000.00');
foreach($money as $mon)
{
echo cleanmoney($mon);
echo "<br>";
}

function cleanmoney($string)
{
$dotpos = strpos($string, '.');
if ($dotpos)
{
$string = substr($string, 0, $dotpos);
}
return str_replace(',', '', $string);
}
?>
I have tried all kinds of different regex recipes and str_replace magic
and nothing seems to be working. I'm frustrated enough at this point that
I just want one of you smart people to write it FOR me!!


And pigs can fly. We're so frustrated because of people that want us to work for
free...


--
Met vriendelijke groeten,
Tim Van Wassenhove <http://www.timvw.info>
Jul 17 '05 #2

P: n/a

Will Clifton wrote:
Hello,

Spent all day yesterday reading about this and I still can't get it.
Perhaps my IQ is not much above room temperature...

My mySQL database is a simple inventory-type database with a "price" field as an integer. The rest of my script relies on price being an integer so I really don't want to change the data type.

I have a page with an HTML
form where the user can type in new entries into the database. In my HTML form, I have the "price" field and I want the user to be able to type the price any way they want. For instance for an item that costs $10,000 they could type:

10000
10,000
10000.00
10,000.00
etc.


Here's some quick & dirty code that does what you want:
<?
$rs = array('$',',');
$strs = array('10000','$10000','$10,000.00','10,000.00');
foreach($strs as $test)
echo $test . '=>' . (int)(str_replace($rs,'',$test)) . "\n";
?>

The output of this is:
10000=>10000
$10000=>10000
$10,000.00=>10000
10,000.00=>10000

Ken

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.