473,320 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Regex or str_replace confusion

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
2 2392
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: lawrence | last post by:
When users enter urls or other long strings it can destroy the formatting of a page. A long url, posted in a comment, can cause page distortions that make the page unreadable, till the website...
1
by: m|sf|t | last post by:
All, If I have this: $xmlstr = str_replace("&", "%amp;%", $xmlstr); How can I use use a regex expression (if it is needed) to do the replace UNLESS the string contains A&E (you know, the TV...
8
by: John Hunter | last post by:
In trying to sdebug why a certain regex wasn't working like I expected it to, I came across this strange (to me) behavior. The file I am trying to match definitely contains many instances of the...
75
by: Xah Lee | last post by:
http://python.org/doc/2.4.1/lib/module-re.html http://python.org/doc/2.4.1/lib/node114.html --------- QUOTE The module defines several functions, constants, and an exception. Some of the...
2
by: Daniel Billingsley | last post by:
First, if MSFT is listening I'll say IMO the MSDN material is sorely lacking in this area... it's just a whole bunch of information thrown at you and you're left to yourself as to organizing it in...
2
by: Tom Jones | last post by:
Hi, I have a component that accepts a string representing a class of files (exactly like those you would pass to the 'dir' dos command, ie. '*.txt', or '???.cpp'). An exception is generated...
6
by: aquaren | last post by:
I am parsing some text files that have all the spacing between words doubled. The space between characters is one instead of zero, and the space between words is two instead of one. I am trying...
1
by: theduffman | last post by:
Hi, I'm trying to write code to update nightly some NHL stats. Everything works, except for names with an accent, e.g. José Théodore. This won't generate a match no matter what I try. I've edited...
4
jamwil
by: jamwil | last post by:
What up friends, I'm using this little function to parse hyperlinks in plain text and convert them into links. It goes like soprivate function hyperlink($text) { // match protocol://address/path/...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.