1

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.

Community
  • 1
  • 1
Nicholas
  • 119
  • 2
  • 17
  • simply you can use regular expressions – Pooya Mar 02 '16 at 08:23
  • 2
    Show us what you have tried till now? – Fakhruddin Ujjainwala Mar 02 '16 at 08:23
  • is this a string or an array? – William J. Mar 02 '16 at 08:28
  • I have tried like this : `$str = str_replace(array('[',']'), '', $_POST['value1']); $str = preg_replace('/\s+/', '', $str); $value1 = preg_split("/[,]+/", $str);` @FakhruddinUjjainwala – Nicholas Mar 02 '16 at 08:40
  • I post this from my android apps as an arraylist @WilliamJanoti – Nicholas Mar 02 '16 at 08:42
  • @Nicholas put what have tried in your question to avoid downvoting :) – Halayem Anis Mar 02 '16 at 08:42
  • @Pooya How was the regular expressions ? – Nicholas Mar 02 '16 at 08:42
  • @FakhruddinUjjainwala Ya I getting with `POST`. – Nicholas Mar 02 '16 at 08:46
  • If you're posting this from your app, why not use this exact format that you're converting to anyway? You could also use JSON. – h2ooooooo Mar 02 '16 at 08:47
  • @h2ooooooo because I was post it from my apps as an arraylist file and I don't know how to split that value – Nicholas Mar 02 '16 at 08:49
  • @Nicholas Don't split it - you have the actual object in your app. Simple iterate through it and *create* this new object. Currently what you're doing is: `create string, modify string` but you should just `create string`. – h2ooooooo Mar 02 '16 at 08:50
  • @h2ooooooo What do you mean ?? Could you give me some example ?? – Nicholas Mar 02 '16 at 08:53
  • @Nicholas You have access to the apps yourself. You have access to the data you're sending yourself. Why not send some different data? Instead of simply converting an ArrayList to a string, go through it with a loop (search *"how to loop through an array list"*) and save all the values to a *new* variables, and this is the one you should send. You can also use pretty much *any* JSON serializer and simply use `json_decode` in php. – h2ooooooo Mar 02 '16 at 08:54
  • @Nicholas: you can search online for regular expressions but php has two good methods preg_match and preg_replace which you can find good documentations on the php.net website – Pooya Mar 02 '16 at 09:00
  • @Nicholas To give you an analogy of what I'm saying, imagine that you're buying a new car. You can buy it in blue and as soon as you get it you can paint it red, or you could've just bought it in red in the first place. Just like you could've just made the data in a usable format *in the first place*. Instead of `list.toString()` all you have to do is go through `list` and create data you can easily read from PHP. Why waste bandwidth of the user because you want to send the text `First`, `Second`, `Third` and `Fourth` when you're throwing it away instantly anyway? – h2ooooooo Mar 02 '16 at 09:04
  • @h2ooooooo Thank you for your advice but now I desperate to make this code work and I need who could help me much. – Nicholas Mar 02 '16 at 09:06
  • @Nicholas I just googled "android json serialize" and [this post came up](http://stackoverflow.com/a/7346833/247893) which explains how to use the Gson library to literally do it all for you. It'll take 2 minutes and you can instantly decode any info from PHP using `json_decode`. – h2ooooooo Mar 02 '16 at 09:08
  • @h2ooooooo Okay, I'll try it. Thank you very much. – Nicholas Mar 02 '16 at 09:24
  • You could use preg_split, but are you really married to that data format? If not then I would suggest switching to one that's relatively easy to process in both PHP and Android, something like JSON or XML. That why you could use json_decode () or DOMDocument on the PHP side – GordonM Mar 02 '16 at 09:38

1 Answers1

0

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);
Antony D'Andrea
  • 991
  • 1
  • 16
  • 35