Skip to content

Latest commit

 

History

History
61 lines (45 loc) · 1.64 KB

README.md

File metadata and controls

61 lines (45 loc) · 1.64 KB

parse_pair(string $string): array

Packagist Version Packagist PHP Version Support GitHub Workflow Status

Why not parse_str?

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

Install

composer require hyqo/parse_pair

Usage

use function Hyqo\Parser\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\nline

If string is invalid, result will be null:

string
foo="bar\"
foo='bar\'
foo="bar""
foo='bar''