Because parse_str
works only with a URL query string format.
So foo="bar"
will be parsed like
array(1) {
["foo"]=>
string(5) ""bar""
}
We have double-quoted value "bar"
. Instead, you can use parse_pair
and if value is a valid double-quoted it will be
expanded
composer require hyqo/pair
use function Hyqo\Pair\parse_pair;
[$key, $value] = parse_pair('foo="bar"');
echo $key; //foo
echo $bar; //bar
If string is valid, it will be parsed:
string | key | value |
---|---|---|
foo= |
foo |
empty string |
foo="" |
foo |
empty string |
foo=bar |
foo |
bar |
foo="bar" |
foo |
bar |
foo='bar' |
foo |
bar |
foo="\"bar\"" |
foo |
"bar" |
foo="\"bar" |
foo |
"bar |
foo="multi\nline" |
foo |
multi line |
foo='multi\nline' |
foo |
multi line |
If string is invalid, result will be null
:
string |
---|
foo="bar\" |
foo='bar\' |
foo="bar"" |
foo='bar'' |