preg Searh and replace problem

Discussion in 'PHP' started by dcsimmons, Jul 2, 2007.

  1. #1
    I have a record set called rsOrders with a table called shipcountry_ord with the countries in it when using a check out procedure.

    I am trying to set a condition for free shipping, but want to only offer in the US, so I am trying to do a search for "US" in the shipcountry_ord table.

    When selecting "United States" from a dropdown list in the checkout procedure, "US" is placed into the shipcountry_ord table.

    I am trying to use the following code to search for the "US" in the shipcountry_ord table.

    preg_replace("/[^\d]/", "", $row_rsOrder['shipcountry_ord'])=='/US/'

    It isn't working, and I do not know what I am doing wrong.
     
    dcsimmons, Jul 2, 2007 IP
  2. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    your pattern matches all characters except numbers. You then replace all found characters with nothing (remove them), and attempt to match numbers against a "US".
    I think what you need is this:
    if ( $row_rsOrder['shipcountry_ord'] == 'US' ) {
      // it is a US order
    }
    PHP:
    you don't need regex, unless you store some other (additional) values in the shipcountry_ord field
     
    UnrealEd, Jul 2, 2007 IP
  3. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #3
    and incase that there is other information within $row_rsOrder['shipcountry_ord'] as well

    
    if ( preg_match("/us/i",$row_rsOrder['shipcountry_ord']) )
    {
       // go
    }
    
    PHP:
     
    ansi, Jul 2, 2007 IP