Regex or str_replace confusion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Will Clifton

    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_ch aracters($price )
    { $price=function _fix_bad_charac ters($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
    wgcabpcli@cox.n et

  • Tim Van Wassenhove

    #2
    Re: Regex or str_replace confusion

    On 2005-05-04, Will Clifton <wgcabpcli@cox. net> wrote:[color=blue]
    > My mySQL database is a simple inventory-type database with a "price" field
    > as an integer.[/color]
    [color=blue]
    > 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.[/color]

    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.
    [color=blue]
    > and with this the user input in "price" is now reformatted so it gets
    > entered into the database as an integer.[/color]

    <?php
    $money = array('10000', '10,000', '10000.00', '10,000.00');
    foreach($money as $mon)
    {
    echo cleanmoney($mon );
    echo "<br>";
    }

    function cleanmoney($str ing)
    {
    $dotpos = strpos($string, '.');
    if ($dotpos)
    {
    $string = substr($string, 0, $dotpos);
    }
    return str_replace(',' , '', $string);
    }
    ?>


    [color=blue]
    > 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!![/color]

    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>

    Comment

    • Ken Robinson

      #3
      Re: Regex or str_replace confusion


      Will Clifton wrote:[color=blue]
      > 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"[/color]
      field[color=blue]
      > as an integer. The rest of my script relies on price being an integer[/color]
      so I[color=blue]
      > 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[/color]
      HTML[color=blue]
      > form, I have the "price" field and I want the user to be able to type[/color]
      the[color=blue]
      > price any way they want. For instance for an item that costs $10,000[/color]
      they[color=blue]
      > could type:
      >
      > 10000
      > 10,000
      > 10000.00
      > 10,000.00
      > etc.
      >[/color]

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

      The output of this is:
      10000=>10000
      $10000=>10000
      $10,000.00=>100 00
      10,000.00=>1000 0

      Ken

      Comment

      Working...