English 中文(简体)
How to get fields from such a string?
原标题:

I m importing some data from a database. The data has been stored by a CMS written in php where I have no control. Here is the data (a dense report from a paypal response):

a:56:{
s:8:"business";s:19:"abcd@abcdefghij.com";
s:14:"receiver_email";s:19:"abcd@abcdefghij.com";
s:11:"receiver_id";s:13:"KVBRSDFJKLWYE";
s:9:"item_name";s:4:"ABCD";
s:11:"item_number";s:1:"7";
s:8:"quantity";s:1:"1";
s:7:"invoice";s:0:"";
s:6:"custom";s:3:"800";
s:4:"memo";s:0:"";
s:3:"tax";s:4:"0.00";
s:12:"option_name1";s:0:"";
s:17:"option_selection1";s:0:"";
s:12:"option_name2";s:0:"";
s:17:"option_selection2";s:0:"";
s:14:"num_cart_items";s:1:"1";
s:8:"mc_gross";s:6:"255.00";
s:6:"mc_fee";s:5:"19.75";
s:11:"mc_currency";s:3:"USD";
s:13:"payment_gross";s:6:"255.00";
s:11:"payment_fee";s:5:"19.75";
s:14:"payment_status";s:9:"Completed";
s:14:"pending_reason";s:0:"";
s:11:"reason_code";s:0:"";
s:12:"payment_date";s:25:"02:11:51 Sep 15, 2006 PDT";
s:6:"txn_id";s:17:"1EG20446283704116";
s:8:"txn_type";s:4:"cart";
s:12:"payment_type";s:7:"instant";
s:10:"first_name";s:5:"abcde";
s:9:"last_name";s:6:"Abcdef";
s:19:"payer_business_name";s:0:"";
s:12:"address_name";s:12:"abcde Abcdef";
s:14:"address_street";s:24:"asdkjhgfs;lkefh sdfkj 21";
s:12:"address_city";s:15:"agflkjsgkjhsddg";
s:13:"address_state";s:3:"HDJ";
s:11:"address_zip";s:5:"64525";
s:20:"address_country_code";s:2:"DE";
s:15:"address_country";s:7:"Germany";
s:14:"address_status";s:11:"unconfirmed";
s:11:"payer_email";s:15:"thgjk@sjghjk.de";
s:8:"payer_id";s:13:"U89LQDFJGKCJG";
s:12:"payer_status";s:8:"verified";
s:9:"member_id";s:3:"800";
s:11:"verify_sign";s:56:"A1JC72dfgkljhdghjwlQocysUrWOAXNp57t4TP6QkJgCt9.qk7A4UuEq";
s:8:"test_ipn";s:0:"";
s:12:"item_number1";s:1:"7";
s:7:"charset";s:12:"windows-1252";
s:11:"mc_shipping";s:4:"0.00";
s:11:"mc_handling";s:4:"0.00";
s:14:"notify_version";s:3:"2.1";
s:12:"mc_handling1";s:4:"0.00";
s:12:"mc_shipping1";s:4:"0.00";
s:10:"item_name1";s:50:"sdlkjgsdfghlsdkgdhlkjsdggkljdfhlkjsddflkhlkdldfkgj";
s:9:"quantity1";s:1:"1";
s:10:"mc_gross_1";s:6:"255.00";
s:17:"residence_country";s:2:"DE";
s:11:"screen_name";s:8:"dfglkjlf";
}

As you can see this is straightforward to read. In my code I would like to grab some of the fields (let s say the value of payment_fee). How can I do that? I guess the best would be to use a regular expression but I m a true rookie with Regexps. Of course I don t want to count the number of colons and quotes to get to the field. I would prefer an automatic way.

Note: I don t care about the s:xx. As you guess it means a string with xx characters and I don t need to validate that.

Thank you for your help.

最佳回答

This regex should allow you to find any field value. Adjust character escaping as needed

var regex = fieldName + "";s:\d*:"([^"]*)" 

(this is c#)

Note that this will return incomplete values if the strings contain a " character...

问题回答

Here s a c# unserialization library for php strings: http://sourceforge.net/projects/csphpserial/

I m not a C# guy, so your mileage may vary, but it looks like it s been around for a while.

This regex will group the payment fee.

 payment_fee";s:d*:"(d*.d*) 

in Python:

s =  s:11:"payment_fee";s:5:"19.75"; 
regex =  payment_fee";s:d*:"(d*.d*) 

payment_fee = re.search(regex, s).groups[0] # returns  19.75 

This appears to be serialized PHP objects. There are probably some Python packages that you can use to unserialize this data - I was able to find one package called phpserialize that might be of interest, but I ve never used it, so I can t comment on how well it works. There might be others out there.

How about something like this:

 string fieldName = "address_status";
 string pattern = String.Format(@".*"{0}";s:[0-9]+:("[^"]*").*", fieldName);
 string value = Regex.Replace(line, pattern, @"$1");




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签