inkd
inkd

Reputation: 1509

Best way to parse this string and create an array from it

I have the follow string:

{item1:test},{item2:hi},{another:please work}

What I want to do is turn it into an array that looks like this:

[item1] => test
[item2] => hi
[another] => please work

Here is the code I am currently using for that (which works):

$vf = '{item1:test},{item2:hi},{another:please work}';
$vf = ltrim($vf, '{');
$vf = rtrim($vf, '}');
$vf = explode('},{', $vf);

foreach ($vf as $vk => $vv)
{
    $ve = explode(':', $vv);
    $vx[$ve[0]] = $ve[1];
}

My concern is; what if the value has a colon in it? For example, lets say that the value for item1 is you:break. That colon is going to make me lose break entirely. What is a better way of coding this in case the value has a colon in it?

Upvotes: 0

Views: 92

Answers (4)

mario
mario

Reputation: 145482

Don't use effing explode for everything.

You can more reliably extract such simple formats with a trivial key:value regex. In particular since you have neat delimiters around them.

And it's far less code:

preg_match_all('/{(\w+):([^}]+)}/', $vf, $match);
$array = array_combine($match[1], $match[2]);

The \w+ just matches an alphanumeric string, and [^}]+ anything that until a closing }. And array_combine more easily turns it into a key=>value array.

Upvotes: 1

Aitch
Aitch

Reputation: 1697

Answering your second question:

If your format crashes with specific content it's bad. I think there are 2 types to work around.

  1. Escape delimiters: that would be, every colon and curly brackets have to be escaped which is strange, so data is delimited with e.g. " and only those quotation marks are escaped (than you have JSON in this case)
  2. Save data lengths: this is a bit how PHP serializes arrays. In that data structure you say, that the next n chars is one token.

The first type is easy to read and manipulate although one have to read the whole file to random access it. The second type would be great for better random accessing if the structure doesn't saves the amount of characters (since in UTF-8 you cannot just skip n chars by not reading them), but saving the amount of bytes to skip. PHP's serialize function produce n == strlen($token), thus I don't know what is the advantage over JSON.

Where possible I try to use JSON for communication between different systems.

Upvotes: 0

Igor Moraru
Igor Moraru

Reputation: 7729

Why not to set a limit on explode function. Like this:

$ve = explode(':', $vv, 2);

This way the string will split only at the first occurrence of a colon.

Upvotes: 2

bloodyKnuckles
bloodyKnuckles

Reputation: 12079

To address the possibility of the values having embedded colons, and for the sake of discussion (not necessarily performance):

$ve = explode(':', $vv);
$key = array_shift($ve);
$vx[$key] = implode(':', $ve);

...grabs the first element of the array, assuming the index will NOT have a colon in it. Then re-joins the rest of the array with colons.

Upvotes: 2

Related Questions