matching a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ajd335
    New Member
    • Apr 2008
    • 123

    matching a string

    Hey ,
    I have a function called get_rate, the code for the get_rate is below. I call that function in which i have added the UPS XML script.And the answer I am getting is as below.
    Code:
    XML Resposne:
    THE Code for Get_rate() function is ,
    function getRate(
                    $w2,
                    $s2,
                    $r1,
                    $p2,
                    $c2,
                    $service_code
            ) {
    
    $this->_xml_request =<<<XML
    <?xml version="1.0"?>
    <AccessRequest xml:lang="en-US">
    <AccessLicenseNumber>$this->_access_key</AccessLicenseNumber>
    <UserId></UserId>
    <Password></Password>
    </AccessRequest>
    <?xml version="1.0"?>
    <RatingServiceSelectionRequest xml:lang="en-US">
       <Request>
          <TransactionReference>
             <CustomerContext>Shipping</CustomerContext>
             <XpciVersion>1.0001</XpciVersion>
          </TransactionReference>
          <RequestAction>$this->_request_option</RequestAction>
          <RequestOption>$this->_request_option</RequestOption>
       </Request>
       <PickupType>
          <Code>01</Code>
       </PickupType>
     <Shipment>
          <Shipper>
            <ShipperNumber></ShipperNumber>
            <Address>
            <City></City>
            <StateProvinceCode></StateProvinceCode>
             <PostalCode></PostalCode>
             <CountryCode></CountryCode>
             </Address>
          </Shipper>
          <ShipTo>
             <Address>
            <StateProvinceCode>$s2</StateProvinceCode>
                <PostalCode>$p2</PostalCode>
                <CountryCode>US</CountryCode>
    <ResidentialAddress>$r1</ResidentialAddress>
             </Address>
          </ShipTo>
          <ShipFrom>
             <Address>
            <StateProvinceCode></StateProvinceCode >
                    <PostalCode></PostalCode>
                    <CountryCode></CountryCode>
             </Address>
          </ShipFrom>
          <Service>
             <Code>03</Code>
          </Service>
          <Package>
             <PackagingType>
                <Code>02</Code>
             </PackagingType>
             <PackageWeight>
                <UnitOfMeasurement>
                   <Code>LBS</Code>
                </UnitOfMeasurement>
                <Weight>{$w2}</Weight>
             </PackageWeight>
          </Package>
          <RateInformation>
      <NegotiatedRatesIndicator/>
          </RateInformation>
       </Shipment>
    </RatingServiceSelectionRequest>
    XML;
    
    AND BELOW is the answer what I got ............
    
    <?xml version="1.0"?><RatingServiceSelectionResponse><Response><TransactionReference><CustomerContext>Shipping</CustomerContext><XpciVersion>1.0001</XpciVersion></TransactionReference><ResponseStatusCode>1</ResponseStatusCode><ResponseStatusDescription>Success</ResponseStatusDescription></Response><RatedShipment><Service><Code>03</Code></Service><RatedShipmentWarning>Your invoice may vary from the displayed reference rates</RatedShipmentWarning><BillingWeight><UnitOfMeasurement><Code>LBS</Code></UnitOfMeasurement><Weight>36.0</Weight></BillingWeight><TransportationCharges><CurrencyCode>USD</CurrencyCode><MonetaryValue>31.14</MonetaryValue></TransportationCharges><ServiceOptionsCharges><CurrencyCode>USD</CurrencyCode><MonetaryValue>0.00</MonetaryValue></ServiceOptionsCharges><TotalCharges><CurrencyCode>USD</CurrencyCode><MonetaryValue>31.14</MonetaryValue></TotalCharges><GuaranteedDaysToDelivery></GuaranteedDaysToDelivery><ScheduledDeliveryTime></ScheduledDeliveryTime><RatedPackage><TransportationCharges><CurrencyCode>USD</CurrencyCode><MonetaryValue>31.14</MonetaryValue></TransportationCharges><ServiceOptionsCharges><CurrencyCode>USD</CurrencyCode><MonetaryValue>0.00</MonetaryValue></ServiceOptionsCharges><TotalCharges><CurrencyCode>USD</CurrencyCode><MonetaryValue>31.14</MonetaryValue></TotalCharges><Weight>36.0</Weight><BillingWeight><UnitOfMeasurement><Code>LBS</Code></UnitOfMeasurement><Weight>36.0</Weight></BillingWeight></RatedPackage></RatedShipment></RatingServiceSelectionResponse>
    My Question is, I want to store the value of MonetaryValue - the cost of the package field which is 31.14 into one variable , so that i can store it in database,
    Do you plz tell me how to do so...I will really be thankful..
    Thanks,
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    Have you heard of http://php.net/simple_xml?

    check the last two depending on how your getting your xml, as a file or as a string.

    then you can access the XML as you would an array, BUT although it acts like it, PHP still treats as an xml object. copy the values you need into your array and you should be set.
    Last edited by Atli; Jun 17 '08, 01:18 AM. Reason: Remved the 'us' from the php.net link. Makes it much faster.

    Comment

    • ajd335
      New Member
      • Apr 2008
      • 123

      #3
      Originally posted by dlite922
      Have you heard of http://php.net/simple_xml?

      check the last two depending on how your getting your xml, as a file or as a string.

      then you can access the XML as you would an array, BUT although it acts like it, PHP still treats as an xml object. copy the values you need into your array and you should be set.

      hey Dlite992,
      Thanks, but i don't have XML file...Actually i have 1 php script page, in which i have included the below XML
      Code:
      I have a function called get_rate, the code for the get_rate is below. 
      XML Resposne:
      THE Code for Get_rate() function is ,
      function getRate(
                      $w2,
                      $s2,
                      $r1,
                      $p2,
                      $c2,
                      $service_code
              ) {
      
      $this->_xml_request =<<<XML
      <?xml version="1.0"?>
      <AccessRequest xml:lang="en-US">
      <AccessLicenseNumber>$this->_access_key</AccessLicenseNumber>
      <UserId></UserId>
      <Password></Password>
      </AccessRequest>
      <?xml version="1.0"?>
      <RatingServiceSelectionRequest xml:lang="en-US">
         <Request>
            <TransactionReference>
               <CustomerContext>Shipping</CustomerContext>
               <XpciVersion>1.0001</XpciVersion>
            </TransactionReference>
            <RequestAction>$this->_request_option</RequestAction>
            <RequestOption>$this->_request_option</RequestOption>
         </Request>
         <PickupType>
            <Code>01</Code>
         </PickupType>
       <Shipment>
            <Shipper>
              <ShipperNumber></ShipperNumber>
              <Address>
              <City></City>
              <StateProvinceCode></StateProvinceCode>
               <PostalCode></PostalCode>
               <CountryCode></CountryCode>
               </Address>
            </Shipper>
            <ShipTo>
               <Address>
              <StateProvinceCode>$s2</StateProvinceCode>
                  <PostalCode>$p2</PostalCode>
                  <CountryCode>US</CountryCode>
      <ResidentialAddress>$r1</ResidentialAddress>
               </Address>
            </ShipTo>
            <ShipFrom>
               <Address>
              <StateProvinceCode></StateProvinceCode >
                      <PostalCode></PostalCode>
                      <CountryCode></CountryCode>
               </Address>
            </ShipFrom>
            <Service>
               <Code>03</Code>
            </Service>
            <Package>
               <PackagingType>
                  <Code>02</Code>
               </PackagingType>
               <PackageWeight>
                  <UnitOfMeasurement>
                     <Code>LBS</Code>
                  </UnitOfMeasurement>
                  <Weight>{$w2}</Weight>
               </PackageWeight>
            </Package>
            <RateInformation>
        <NegotiatedRatesIndicator/>
            </RateInformation>
         </Shipment>
      </RatingServiceSelectionRequest>
      XML;
      AND BELOW is the answer what I got , that is generated by UPS API which i am calling in the program , and the resulting response is ,......
      Code:
      <?xml version="1.0"?><RatingServiceSelectionResponse><Response><TransactionReference><CustomerContext>Shipping</CustomerContext><XpciVersion>1.0001</XpciVersion></TransactionReference><ResponseStatusCode>1</ResponseStatusCode><ResponseStatusDescription>Success</ResponseStatusDescription></Response><RatedShipment><Service><Code>03</Code></Service><RatedShipmentWarning>Your invoice may vary from the displayed reference rates</RatedShipmentWarning><BillingWeight><UnitOfMeasurement><Code>LBS</Code></UnitOfMeasurement><Weight>36.0</Weight></BillingWeight><TransportationCharges><CurrencyCode>USD</CurrencyCode><MonetaryValue>31.14</MonetaryValue></TransportationCharges><ServiceOptionsCharges><CurrencyCode>USD</CurrencyCode><MonetaryValue>0.00</MonetaryValue></ServiceOptionsCharges><TotalCharges><CurrencyCode>USD</CurrencyCode><MonetaryValue>31.14</MonetaryValue></TotalCharges><GuaranteedDaysToDelivery></GuaranteedDaysToDelivery><ScheduledDeliveryTime></ScheduledDeliveryTime><RatedPackage><TransportationCharges><CurrencyCode>USD</CurrencyCode><MonetaryValue>31.14</MonetaryValue></TransportationCharges><ServiceOptionsCharges><CurrencyCode>USD</CurrencyCode><MonetaryValue>0.00</MonetaryValue></ServiceOptionsCharges><TotalCharges><CurrencyCode>USD</CurrencyCode><MonetaryValue>31.14</MonetaryValue></TotalCharges><Weight>36.0</Weight><BillingWeight><UnitOfMeasurement><Code>LBS</Code></UnitOfMeasurement><Weight>36.0</Weight></BillingWeight></RatedPackage></RatedShipment></RatingServiceSelectionResponse>
      ...
      i wanted to have the MonetaryValue filed to be stored into one variable.....(i .e 31.14 to be stored in a variable).

      <TotalCharges>< CurrencyCode>US D</CurrencyCode><M onetaryValue>31 .14</MonetaryValue></TotalCharges>
      [/code]

      I have used xmlrpc_decode , but dint work..can you please suggest me what can be done?

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        You have posted this three times.

        Give people a chance to answer your questions!

        Comment

        • ajd335
          New Member
          • Apr 2008
          • 123

          #5
          Originally posted by markusn00b
          You have posted this three times.

          Give people a chance to answer your questions!
          Hi Markus ,
          yeah I posted the same but , the other times I need some XML to PHP parsing method...But i solved that , and now i needed to just have a string matching function , so i again posted that ....The purpose is different in all..

          Comment

          • nashruddin
            New Member
            • Jun 2008
            • 25

            #6
            Use SimpleXML (PHP5):

            Code:
            <?php
            /* $xmlstr is the XML string */
            $xml = simplexml_load_string($xmlstr);
            
            $monetary_value = $xml->RatedShipment->TotalCharges->MonetaryValue;
            
            print $monetary_value; /* will print: 31.14 */
            ?>

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              Originally posted by ajd335
              I want to store the value of
              <TotalCharges>< CurrencyCode>US D</CurrencyCode><M onetaryValue>31 .14</MonetaryValue></TotalCharges>
              value of MonetaryValue field which is 31.14 in one variable..
              How can i use preg_match or ereg here ???
              I guess you could use regex, somewhat like this:
              [code=php]
              // Replace 'tagname' with the tag you want to find
              $regex = '/\<tagname\>([\d\.]+)\<\/tagname\>/is'
              preg_match_all( $regex, $string, $matches);
              print_r($matche s);
              [/code]
              But the preferred method would probably be to use the XML functions, as nashruddin suggested.

              Comment

              • pbmods
                Recognized Expert Expert
                • Apr 2007
                • 5821

                #8
                Heya, AJD.

                Play around with SimpleXML. You should be able to do something like this:

                [code=php]
                $parser = new SimpleXMLElemen t($xmlData);
                echo $parser->RatingServiceS electionRespons e->Response->RatedShipmen t->Transportation Charges->MonetaryValu e;
                [/code]

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #9
                  Originally posted by ajd335
                  Hi Markus ,
                  yeah I posted the same but , the other times I need some XML to PHP parsing method...But i solved that , and now i needed to just have a string matching function , so i again posted that ....The purpose is different in all..
                  The three threads you posted were pretty much identical. They were all asking for a solution to the same problem, even if the means to which you wanted to reach this solution was slightly different for each one.

                  They were, in fact, so similar that they three different people provided pretty much the same solution to all of them.

                  So, to avoid confusion, I have merged them all into this one.

                  In the future, please try not to post the same problem multiple times, even if you are looking for different ways to fix it. It is much simpler to just ask again in the same thread. That way people will at least see what you have already been doing.

                  Thanks.
                  Moderator

                  Comment

                  • ajd335
                    New Member
                    • Apr 2008
                    • 123

                    #10
                    Hey all..
                    Thanks for the help....Now its working fine....

                    Comment

                    Working...