Use explode () , you can use regexp for it, but it is simple enough without overhead.
$data = array(); foreach (explode("\n", $dataString) as $cLine) { list ($cKey, $cValue) = explode(':', $cLine, 2); $data[$cKey] = $cValue; }
As mentioned in the comments, if the data comes from a Windows / DOS environment, it may have CRLF lines, adding the following line before foreach()
resolves this.
$dataString = str_replace("\r", "", $dataString); // remove possible \r characters
The alternative with regexp can be quite enjoyable using preg_match_all () and array_combine () :
$matches = array(); preg_match_all('/^(.+?):(.+)$/m', $dataString, $matches); $data = array_combine($matches[1], $matches[2]);
Orbling
source share