473,473 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

matching a string

123 New Member
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.
Expand|Select|Wrap|Line Numbers
  1. XML Resposne:
  2. THE Code for Get_rate() function is ,
  3. function getRate(
  4.                 $w2,
  5.                 $s2,
  6.                 $r1,
  7.                 $p2,
  8.                 $c2,
  9.                 $service_code
  10.         ) {
  11.  
  12. $this->_xml_request =<<<XML
  13. <?xml version="1.0"?>
  14. <AccessRequest xml:lang="en-US">
  15. <AccessLicenseNumber>$this->_access_key</AccessLicenseNumber>
  16. <UserId></UserId>
  17. <Password></Password>
  18. </AccessRequest>
  19. <?xml version="1.0"?>
  20. <RatingServiceSelectionRequest xml:lang="en-US">
  21.    <Request>
  22.       <TransactionReference>
  23.          <CustomerContext>Shipping</CustomerContext>
  24.          <XpciVersion>1.0001</XpciVersion>
  25.       </TransactionReference>
  26.       <RequestAction>$this->_request_option</RequestAction>
  27.       <RequestOption>$this->_request_option</RequestOption>
  28.    </Request>
  29.    <PickupType>
  30.       <Code>01</Code>
  31.    </PickupType>
  32.  <Shipment>
  33.       <Shipper>
  34.         <ShipperNumber></ShipperNumber>
  35.         <Address>
  36.         <City></City>
  37.         <StateProvinceCode></StateProvinceCode>
  38.          <PostalCode></PostalCode>
  39.          <CountryCode></CountryCode>
  40.          </Address>
  41.       </Shipper>
  42.       <ShipTo>
  43.          <Address>
  44.         <StateProvinceCode>$s2</StateProvinceCode>
  45.             <PostalCode>$p2</PostalCode>
  46.             <CountryCode>US</CountryCode>
  47. <ResidentialAddress>$r1</ResidentialAddress>
  48.          </Address>
  49.       </ShipTo>
  50.       <ShipFrom>
  51.          <Address>
  52.         <StateProvinceCode></StateProvinceCode >
  53.                 <PostalCode></PostalCode>
  54.                 <CountryCode></CountryCode>
  55.          </Address>
  56.       </ShipFrom>
  57.       <Service>
  58.          <Code>03</Code>
  59.       </Service>
  60.       <Package>
  61.          <PackagingType>
  62.             <Code>02</Code>
  63.          </PackagingType>
  64.          <PackageWeight>
  65.             <UnitOfMeasurement>
  66.                <Code>LBS</Code>
  67.             </UnitOfMeasurement>
  68.             <Weight>{$w2}</Weight>
  69.          </PackageWeight>
  70.       </Package>
  71.       <RateInformation>
  72.   <NegotiatedRatesIndicator/>
  73.       </RateInformation>
  74.    </Shipment>
  75. </RatingServiceSelectionRequest>
  76. XML;
  77.  
  78. AND BELOW is the answer what I got ............
  79.  
  80. <?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>
  81.  
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,
Jun 13 '08 #1
9 1634
dlite922
1,584 Recognized Expert Top Contributor
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.
Jun 17 '08 #2
ajd335
123 New Member
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
Expand|Select|Wrap|Line Numbers
  1.  
  2. I have a function called get_rate, the code for the get_rate is below. 
  3. XML Resposne:
  4. THE Code for Get_rate() function is ,
  5. function getRate(
  6.                 $w2,
  7.                 $s2,
  8.                 $r1,
  9.                 $p2,
  10.                 $c2,
  11.                 $service_code
  12.         ) {
  13.  
  14. $this->_xml_request =<<<XML
  15. <?xml version="1.0"?>
  16. <AccessRequest xml:lang="en-US">
  17. <AccessLicenseNumber>$this->_access_key</AccessLicenseNumber>
  18. <UserId></UserId>
  19. <Password></Password>
  20. </AccessRequest>
  21. <?xml version="1.0"?>
  22. <RatingServiceSelectionRequest xml:lang="en-US">
  23.    <Request>
  24.       <TransactionReference>
  25.          <CustomerContext>Shipping</CustomerContext>
  26.          <XpciVersion>1.0001</XpciVersion>
  27.       </TransactionReference>
  28.       <RequestAction>$this->_request_option</RequestAction>
  29.       <RequestOption>$this->_request_option</RequestOption>
  30.    </Request>
  31.    <PickupType>
  32.       <Code>01</Code>
  33.    </PickupType>
  34.  <Shipment>
  35.       <Shipper>
  36.         <ShipperNumber></ShipperNumber>
  37.         <Address>
  38.         <City></City>
  39.         <StateProvinceCode></StateProvinceCode>
  40.          <PostalCode></PostalCode>
  41.          <CountryCode></CountryCode>
  42.          </Address>
  43.       </Shipper>
  44.       <ShipTo>
  45.          <Address>
  46.         <StateProvinceCode>$s2</StateProvinceCode>
  47.             <PostalCode>$p2</PostalCode>
  48.             <CountryCode>US</CountryCode>
  49. <ResidentialAddress>$r1</ResidentialAddress>
  50.          </Address>
  51.       </ShipTo>
  52.       <ShipFrom>
  53.          <Address>
  54.         <StateProvinceCode></StateProvinceCode >
  55.                 <PostalCode></PostalCode>
  56.                 <CountryCode></CountryCode>
  57.          </Address>
  58.       </ShipFrom>
  59.       <Service>
  60.          <Code>03</Code>
  61.       </Service>
  62.       <Package>
  63.          <PackagingType>
  64.             <Code>02</Code>
  65.          </PackagingType>
  66.          <PackageWeight>
  67.             <UnitOfMeasurement>
  68.                <Code>LBS</Code>
  69.             </UnitOfMeasurement>
  70.             <Weight>{$w2}</Weight>
  71.          </PackageWeight>
  72.       </Package>
  73.       <RateInformation>
  74.   <NegotiatedRatesIndicator/>
  75.       </RateInformation>
  76.    </Shipment>
  77. </RatingServiceSelectionRequest>
  78. XML;
  79.  
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 ,......
Expand|Select|Wrap|Line Numbers
  1. <?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>
  2.  
...
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>USD</CurrencyCode><MonetaryValue>31.14</MonetaryValue></TotalCharges>
[/code]

I have used xmlrpc_decode , but dint work..can you please suggest me what can be done?
Jun 17 '08 #3
Markus
6,050 Recognized Expert Expert
You have posted this three times.

Give people a chance to answer your questions!
Jun 17 '08 #4
ajd335
123 New Member
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..
Jun 17 '08 #5
nashruddin
25 New Member
Use SimpleXML (PHP5):

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /* $xmlstr is the XML string */
  3. $xml = simplexml_load_string($xmlstr);
  4.  
  5. $monetary_value = $xml->RatedShipment->TotalCharges->MonetaryValue;
  6.  
  7. print $monetary_value; /* will print: 31.14 */
  8. ?>
  9.  
Jun 20 '08 #6
Atli
5,058 Recognized Expert Expert
I want to store the value of
<TotalCharges><CurrencyCode>USD</CurrencyCode><MonetaryValue>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:
Expand|Select|Wrap|Line Numbers
  1. // Replace 'tagname' with the tag you want to find
  2. $regex = '/\<tagname\>([\d\.]+)\<\/tagname\>/is'
  3. preg_match_all($regex, $string, $matches);
  4. print_r($matches);
  5.  
But the preferred method would probably be to use the XML functions, as nashruddin suggested.
Jun 20 '08 #7
pbmods
5,821 Recognized Expert Expert
Heya, AJD.

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

Expand|Select|Wrap|Line Numbers
  1. $parser = new SimpleXMLElement($xmlData);
  2. echo $parser->RatingServiceSelectionResponse->Response->RatedShipment->TransportationCharges->MonetaryValue;
  3.  
Jun 22 '08 #8
Atli
5,058 Recognized Expert Expert
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
Jun 22 '08 #9
ajd335
123 New Member
Hey all..
Thanks for the help....Now its working fine....
Jun 23 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # Matching string patterns # # Sometimes you want to know if a string is of # particular pattern. Let's say in your website # you have converted all images...
7
by: Niko | last post by:
Hi , I am writing script using multiple pattern matching. I need to replace the $_ =~ few times. Example: sub xx { open (TEMPF,"> $T_FILE");
1
by: Henry | last post by:
I have a table that stores a list of zip codes using a varchar column type, and I need to perform some string prefix pattern matching search. Let's say that I have the columns: 94000-1235 94001...
3
by: Day Of The Eagle | last post by:
Jeff_Relf wrote: > ...yet you don't even know what RegEx is. > I'm looking at the source code for mono's Regex implementation right now. You can download that source here ( use the class...
0
by: Tom Warren | last post by:
I found a c program called similcmp on the net and converted it to vba if anybody wants it. I'll post the technical research on it if there is any call for it. It looks like it could be a useful...
10
by: javuchi | last post by:
I'm searching for a library which makes aproximative string matching, for example, searching in a dictionary the word "motorcycle", but returns similar strings like "motorcicle". Is there such a...
5
by: olaufr | last post by:
Hi, I'd need to perform simple pattern matching within a string using a list of possible patterns. For example, I want to know if the substring starting at position n matches any of the string I...
7
by: Kevin CH | last post by:
Hi, I'm currently running into a confusion on regex and hopefully you guys can clear it up for me. Suppose I have a regular expression (0|(1(01*0)*1))* and two test strings: 110_1011101_ and...
8
by: regis | last post by:
Greetings, about scanf matching nonempty sequences using the "%" matches a nonempty sequence of anything except '-' "%" matches a nonempty sequence of anything except ']" matches a nonempty...
11
by: tech | last post by:
Hi, I need a function to specify a match pattern including using wildcard characters as below to find chars in a std::string. The match pattern can contain the wildcard characters "*" and "?",...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.