Jump to content

Recommended Posts

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  :idea:

Link to comment
https://forums.phpfreaks.com/topic/275181-parsing/
Share on other sites

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);
            }
        }
    }
}
Link to comment
https://forums.phpfreaks.com/topic/275181-parsing/#findComment-1416245
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/275181-parsing/#findComment-1416359
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/275181-parsing/#findComment-1416361
Share on other sites

$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 by haku
Link to comment
https://forums.phpfreaks.com/topic/275181-parsing/#findComment-1416364
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.