DuaneCawaling Posted March 3, 2013 Share Posted March 3, 2013 hi, my group is developing an sms feture. in this feature a certain sms message should be messaged by the user/subscriber. one of the sub-feature is ordering. a certain text patter should be followed: SMART<space>WAL<space>BUY<space>"order" example: SMART WAL BUY 2-A,1-B,3-D in 2-A, whew 2 is the quantity and A is the item tag what would be the best solution to use the explode(); function? first is, there are other keywords used like "BAL" or "MENU" other than "BUY". second after the "BUY" keyword, i would like to use the "-" as a delimiter to separate the quantity and item tag. please share your knowledge on the topic comments and suggestions are ALWAYS welcome Quote Link to comment https://forums.phpfreaks.com/topic/275181-parsing/ Share on other sites More sharing options...
teynon Posted March 3, 2013 Share Posted March 3, 2013 You should explode this three times. First, you are getting the parameters. The second time, getting the values of what they are buying. The third, you are breaking down the details of the item. $string = "SMART WAL BUY 2-A,1-B,3-D"; $parameters = explode(" ", $string); if ($parameters[0] == "SMART") { if ($parameters[1] == "WAL") { if ($parameters[2] == "BUY") { $purchases = explode(",", $parameters[3]); foreach ($purchases as $purchase) { $itemInfo = explode("-", $purchase"); addToOrder($itemInfo[0], $itemInfo[1]); // Create function for saving order, addToOrder($qty, $item); } } } } Quote Link to comment https://forums.phpfreaks.com/topic/275181-parsing/#findComment-1416245 Share on other sites More sharing options...
AyKay47 Posted March 3, 2013 Share Posted March 3, 2013 Depending on how complex the subject string can get, I might look into using a PCRE for this. Could save you a lot of coding. Quote Link to comment https://forums.phpfreaks.com/topic/275181-parsing/#findComment-1416248 Share on other sites More sharing options...
haku Posted March 4, 2013 Share Posted March 4, 2013 what would be the best solutionHard to tell, when you haven't told us what you are trying to do. You described some text, but didn't say what you want to do with it. Are you testing it to see if it's valid? Converting it to something else? Something else? Quote Link to comment https://forums.phpfreaks.com/topic/275181-parsing/#findComment-1416354 Share on other sites More sharing options...
DuaneCawaling Posted March 4, 2013 Author Share Posted March 4, 2013 thank you for the advice, i've followed the tips above. the next concern now is accesing 3 different database. lets say A,B,D are present in three different databases. and i want to retrieve their corresponding prices and total them. what would be the fates and efficient way?am i going to code 3 different mysql_query's? Quote Link to comment https://forums.phpfreaks.com/topic/275181-parsing/#findComment-1416359 Share on other sites More sharing options...
DuaneCawaling Posted March 4, 2013 Author Share Posted March 4, 2013 Hard to tell, when you haven't told us what you are trying to do. You described some text, but didn't say what you want to do with it. Are you testing it to see if it's valid? Converting it to something else? Something else? i wanna compute their total. the items' information is in a database. and by parsing them as such i can now get each of their prices and add them. but i dont know on what should i code first. Quote Link to comment https://forums.phpfreaks.com/topic/275181-parsing/#findComment-1416361 Share on other sites More sharing options...
haku Posted March 4, 2013 Share Posted March 4, 2013 (edited) $string = "SMART WAL BUY 2-A,1-B,3-D"; preg_match('/^SMART WAL (BUY|BAL|MENU) (.+?)$/', $string, $matches); if(isset($matches[2])) { $elements = explode(',', $matches[2]); foreach($element as $e) { $pieces = explode('-', trim($e)); $quantity = $pieces[0]; $tag = $pieces[1]; // do something with the $quantity and $tag here } } Edited March 4, 2013 by haku Quote Link to comment https://forums.phpfreaks.com/topic/275181-parsing/#findComment-1416364 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.