hyqo/pair

1.0.0 2023-03-05 17:20 UTC

This package is auto-updated.

Last update: 2024-06-05 20:03:32 UTC


README

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/pair

Usage

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''