开发者

how to change string with delimiter into array?

开发者 https://www.devze.com 2023-02-18 00:01 出处:网络
I am having this string.........开发者_如何学JAVA 0&39avfy;&39avfy;task&39avfy;rose&39avfy;duration&39avfy;1.25&39avfy;user&39avfy;15&39avfy;1&39avfy;&39avfy;t

I am having this string.........

开发者_如何学JAVA
0&39avfy;&39avfy;task&39avfy;rose&39avfy;duration&39avfy;1.25&39avfy;user&39avfy;15&39avfy;1&39avfy;&39avfy;task&39avfy;daisy&39avfy;duration&39avfy;0.75&39avfy;user&39avfy;25&39avfy;2&39avfy;&39avfy;task&39avfy;orchid&39avfy;duration&39avfy;1.15&39avfy;user&39avfy;7

I need array like this..........

array( 
               array( "task" => "rose", 
                      "duration" => 1.25,
                      "user" => 15 
                    ),
               array( "task" => "daisy", 
                      "duration" => 0.75,
                      "user" => 25,
                    ),
               array( "task" => "orchid", 
                      "duration" => 1.15,
                      "user" => 7 
                    )
             );


Well, since it is uniformly ordered you can use this approach:

 preg_match_all('#
                  task      &39avfy;  (?<task> [^&]*)  .*?
                  duration  &39avfy;  (?<duration> [^&]*) .*?
                  user      &39avfy;  (?<user> [^&]*)
              \K #x',
     $str, $matches, PREG_SET_ORDER);

It will have some numeric array entries, but you can filter them out. More importantly, this is easier to adapt should your delimiter change.

print_r($matches);

[0] => Array
    (
        [0] => 
        [task] => rose
        [1] => rose
        [duration] => 1.25
        [2] => 1.25
        [user] => 15
        [3] => 15
    )

[1] => Array
    (
        [0] => 
        [task] => daisy
        [1] => daisy
        [duration] => 0.75
        [2] => 0.75
        [user] => 25
        [3] => 25
    )


At least this solves your problem.

<?php

$in = '0&39avfy;&39avfy;task&39avfy;rose&39avfy;duration&39avfy;1.25&39avfy;user&39avfy;15&39avfy;1&39avfy;&39avfy;task&39avfy;daisy&39avfy;duration&39avfy;0.75&39avfy;user&39avfy;25&39avfy;2&39avfy;&39avfy;task&39avfy;orchid&39avfy;duration&39avfy;1.15&39avfy;user&39avfy;7';

$arr = explode('&39avfy;',$in);
$out = array();
$i = 0;
foreach($arr as $a){
    switch ($i) {
        case 0:
            if(isset($tmp)) { $out[] = $tmp; }
            break;
        case 3:
            $tmp["task"] = $a;
            break;
        case 5:
            $tmp["duration"] = $a;
            break;
        case 7:
            $tmp["user"] = $a;
            break;
        default:
    }
    $i++;
    $i%=8;
}
if($i == 0 && isset($tmp)) { $out[] = $tmp; }
var_dump($out);

array(3) {
  [0]=>
  array(3) {
    ["task"]=>
    string(4) "rose"
    ["duration"]=>
    string(4) "1.25"
    ["user"]=>
    string(2) "15"
  }
  [1]=>
  array(3) {
    ["task"]=>
    string(5) "daisy"
    ["duration"]=>
    string(4) "0.75"
    ["user"]=>
    string(2) "25"
  }
  [2]=>
  array(3) {
    ["task"]=>
    string(6) "orchid"
    ["duration"]=>
    string(4) "1.15"
    ["user"]=>
    string(1) "7"
  }
}

this code leads to the same result:

<?php

$in = '0&39avfy;&39avfy;task&39avfy;rose&39avfy;duration&39avfy;1.25&39avfy;user&39avfy;15&39avfy;1&39avfy;&39avfy;task&39avfy;daisy&39avfy;duration&39avfy;0.75&39avfy;user&39avfy;25&39avfy;2&39avfy;&39avfy;task&39avfy;orchid&39avfy;duration&39avfy;1.15&39avfy;user&39avfy;7';

$matches = array();
$regex = '/(\\d)&39avfy;&39avfy;task&39avfy;(.+?)&39avfy;duration&39avfy;(\\d+\\.\\d+)&39avfy;user&39avfy;(\\d+)/';
preg_match_all($regex, $in, $matches, PREG_SET_ORDER  );
$out = array();
foreach($matches as $m){
    $tmp = array();
    $tmp['task'] = $m[2];
    $tmp['duration'] = $m[3];
    $tmp['user'] = $m[4];
    $out[$m[1]] = $tmp;
}
var_dump($out);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号