Unfortunately there is no a built in PHP function that natively handles delimited arguments like that. However, you can build one pretty quickly using a little regex and a bit of array walking. This is just an example and only works with the type of string you provided. Any extra conditions will need to be added to the regex to make sure it matches the patterns correctly. You can easily call this function as you iterate through your text file.
/**
* Parse a string of settings which are delimited by equal signs and seperated by white
* space, and where text strings are escaped by double quotes.
*
* @param String $string String to parse
* @return Array The parsed array of key/values
*/
function parse_options($string){
// init the parsed option container
$options = array();
// search for any combination of word=word or word="anything"
if(preg_match_all('/(\w+)=(\w+)|(\w+)="(.*)"/', $string, $matches)){
// if we have at least one match, we walk the resulting array (index 0)
array_walk_recursive(
$matches[0],
function($item) use (&$options){
// trim out the " and explode at the =
list($key, $val) = explode('=', str_replace('"', '', $item));
$options[$key] = $val;
}
);
}
return $options;
}
// test it
$string = 'host=db test="test test" blah=123';
if(!($parsed = parse_options($string))){
echo "Failed to parse option string: '$string'\n";
} else {
print_r($parsed);
}