Skip to content

hyqo/pair

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages