Questions about the group of string variables

Source: Internet
Author: User
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"
}
}
*/
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.