Jeff_Mccome Posted May 17, 2015 Share Posted May 17, 2015 Hi guy, I have this combination of record number, ID and qty and I need help sort them out and fill in column as follow: (141442872453(2)) NV4852-SD (2) I need to get the following: record number: "141442872453" ID #: "NV4852-SD" QTY: "2" Thanks Quote Link to comment https://forums.phpfreaks.com/topic/296371-need-help-get-record-number-id-and-qty-of-an-item/ Share on other sites More sharing options...
ginerjm Posted May 17, 2015 Share Posted May 17, 2015 Lookup the explode function in the php manual. That will help you break apart the string into 3 strings and then you can break apart the first string to drop the last (2) part. Same for the 3rd string. Quote Link to comment https://forums.phpfreaks.com/topic/296371-need-help-get-record-number-id-and-qty-of-an-item/#findComment-1512110 Share on other sites More sharing options...
Jeff_Mccome Posted May 18, 2015 Author Share Posted May 18, 2015 Hi Ginerjm, Thanks for the reply. I'm started to learn PHP. Can you help me a few code...I will start from there? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/296371-need-help-get-record-number-id-and-qty-of-an-item/#findComment-1512112 Share on other sites More sharing options...
ginerjm Posted May 18, 2015 Share Posted May 18, 2015 Tis better that you read the manual and Learn from your trial and error(s) than for me (or someone else here) writing it for you. Good luck and have fun. Besides if I show you how to separate out your 3 values from this string what the heck are you going to then? Have someone write the next piece of the puzzle for you? C'mon! 1 Quote Link to comment https://forums.phpfreaks.com/topic/296371-need-help-get-record-number-id-and-qty-of-an-item/#findComment-1512113 Share on other sites More sharing options...
Psycho Posted May 18, 2015 Share Posted May 18, 2015 I don't think this is a scenario where I would use explode. Rather, I think, regular expression would be a better solution. Having said that, you show a sample string that contains 4 pieces of information, yet you only show three values. Do the two instances of "2" in the string both represent the quantity? Quote Link to comment https://forums.phpfreaks.com/topic/296371-need-help-get-record-number-id-and-qty-of-an-item/#findComment-1512114 Share on other sites More sharing options...
Barand Posted May 18, 2015 Share Posted May 18, 2015 Alternative to regex $a = '(141442872453(2)) NV4852-SD (2)'; $b = str_replace(['(',')'], ' ', $a); $data = sscanf($b, ' %s %d %s %d'); echo '<pre>',print_r($data, true),'</pre>'; giving Array ( [0] => 141442872453 [1] => 2 [2] => NV4852-SD [3] => 2 ) Quote Link to comment https://forums.phpfreaks.com/topic/296371-need-help-get-record-number-id-and-qty-of-an-item/#findComment-1512126 Share on other sites More sharing options...
grissom Posted May 18, 2015 Share Posted May 18, 2015 Barand's solution is the most elegant and I like it a lot! But if you are a newbie, then the explode function is nice and simple, provided that the three values in your string are always separated by spaces. If that is the case, then $string = '(141442872453(2)) NV4852-SD (2)'; $parts = explode(' ', $string); Will split the string wherever there is a space and put the parts into an array called $parts so : $parts[0] = '(141442872453(2))' $parts[1] = 'NV4852-SD' $parts[2] = '(2)' The ID number is just $parts[1] To get the record number and quantity, you need to do a little more work. For that, you could try a function called substr ... like the others, I'll leave you now to look that one up in the manual and see if you can take it forward yourself. Good luck ! Quote Link to comment https://forums.phpfreaks.com/topic/296371-need-help-get-record-number-id-and-qty-of-an-item/#findComment-1512157 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.