fork download
  1. <?php
  2. $string ='FooID: 123456
  3. Name: Chuck
  4. When: 01/02/2013 01:23:45
  5. InternalID: 789654
  6. User Message: Hello,
  7. this is nillable, but can be quite long. Text can be spread out over many lines
  8. And can start with any number of \n\'s. It can be empty, too. Can contain colons (:), too
  9. Yellow:cool';
  10.  
  11. $array = array();
  12. preg_replace_callback('#^(.*?):(.*)|.*$#m', function($m)use(&$array){
  13. static $last_key = '';
  14. if(isset($m[1])){
  15. $array[$m[1]] = $m[2];
  16. $last_key = $m[1];
  17. }else{
  18. $array[$last_key] .= PHP_EOL . $m[0];
  19. }
  20. }, $string);
  21. print_r($array);
  22. ?>
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
Array
(
    [FooID] =>  123456

    [Name] =>  Chuck

    [When] =>  01/02/2013 01:23:45

    [InternalID] =>  789654

    [User Message] =>  Hello,

this is nillable, but can be quite long. Text can be spread out over many lines

    [And can start with any number of \n's. It can be empty, too. Can contain colons (] => ), too

    [Yellow] => cool

)