dqask02082 2016-03-02 08:18
浏览 64

我怎么能在PHP中拆分价值?

I have this value like this in PHP file :

{First=itema,Fourth=10000.0,Second=10,Third=1000},{First=itemb,Fourth=12000.0,Second=12,Third=1000}

How can I split this values 'till I get values like this :

{itema,10000,10,100} AND {itemb,12000,12,1000}

I got that value from method POST in my android apps and I get in my PHP file like this :

<?php


$str = str_replace(array('[',']'), '', $_POST['value1']);
$str = preg_replace('/\s+/', '', $str);

echo $str;

?>

And, this is my code in android apps :

try {

            httpclient = new DefaultHttpClient();
            httppost = new HttpPost("http://192.168.107.87/final-sis/order.php");

            nameValuePairs = new ArrayList<NameValuePair>(2);

            nameValuePairs.add(new BasicNameValuePair("value1", list.toString()));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            response=httpclient.execute(httppost);
            Log.d("Value Price: ", httppost.toString());
            HttpEntity entity=response.getEntity();
            feedback = EntityUtils.toString(entity).trim();
            Log.d("FeedBack", feedback);

        }catch (Exception e){

        }

This is link about my code completely :

How can I store my ArrayList values to MySQL databases??

I have been asking before but I couldn't found best way.

Thank you.

  • 写回答

1条回答 默认 最新

  • dongzhijing8202 2016-03-02 09:35
    关注

    This will produce the desired string. I use some regular expressions first to split the initial string and then to grab each value.

    $s = '{First=itema,Fourth=10000.0,Second=10,Third=1000},{First=itemb,Fourth=12000.0,Second=12,Third=1000}';
    
    preg_match_all('/\{[^}]*}/', $s, $m);
    

    At this point $m contains:

    array(1) {
      [0]=>
      array(2) {
        [0]=>
        string(49) "{First=itema,Fourth=10000.0,Second=10,Third=1000}"
        [1]=>
        string(49) "{First=itemb,Fourth=12000.0,Second=12,Third=1000}"
      }
    }
    

    Then we loop each part. The next regex:

    '/\=([^\,]+)/'
    

    This says, grab all the text that is in between an equals sign and a comma and put it into a capturing group.

    Then we just implode the matches.

    $parts = array();
    foreach($m[0] as $match) {
       $t = trim($match, '{}');
       preg_match_all('/\=([^\,]+)/', $t, $m2);
       $parts[] = '{'.implode($m2[1], ',').'}';
    }
    
    $finalString = implode($parts, ' AND ');
    var_dump($finalString);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染