Asking questions about string variable groups
$s = "Serial number: JH0019 Status: Processing Grade: High time: 2013-05-28-12:55:29"
How to convert the array structure to
Array
[Serial number]=>jh0019
[Status]=> in Process
[Level]=> High
[Time]=>2013-05-28 12:55:29
)
My train of thought is:
Change the "" First |
Then the | | Change |
Re: | change:
Re-put |: change:
But the space in the time and the space in the middle of the serial number I don't know how to get rid of it.
Finally with
$a =array ();
foreach (Explode (' | ', $s) as $s) {
List ($k, $v) =explode (': ', $s);
$a [$k]= $v;}
I have coase dizzy, how can get results, please help, thank you
Share to:
------Solution--------------------
$s = "Serial number: JH0019 Status: Processing grade: High time: 2013-05-28 12:55:29";
$a = Preg_split ('/:\s* ([\d:-]+
------Solution--------------------
------Solution--------------------
Preg_split_no_empty);
foreach (Array_chunk ($a, 2) as $r) {
$res [Preg_replace ('/\s
------Solution--------------------
/', ', $r [0])] = $r [1];
}
Print_r ($res);
Array
(
[Serial number] = JH0019
[Status] = in process
[Rank] = high
[Time] = 2013-05-28 12:55:29
)
------Solution--------------------
$s = "Serial number: JH0019 Status: Processing Grade: High time: 2013-05-28 12:55:29";
Preg_match_all ('/([^:]+): ([^\s]+)/', $s, $m);
Echo ' Var_dump ($m);
/**
Output:
Array (3) {
[0]=>
Array (3) {
[0]=>
String (20) "Serial Number: JH0019"
[ 1]=>
String (24) "Status: In Process"
[2]=>
String (15) "Level: High"
}
[1]=>
Array (3) {
[0]=>
String (11) "Serial number"
[1]=>
String (12) "state"
[2]=>
String (9) "level "
}
[2]=>
Array (3) {
[0]=>
String (6)" JH0019 "
[1]=>
String (9)" In Process "
[2]=>
String (3) "High"
}
}
*/