kuzey beytar
kuzey beytar

Reputation: 3226

Parse a string with PHP

I keep product options as string in DB, and I need to parse it (string to array).

String is:

{option_name:option_id{variant_id:variant_name}}

How can I parse it with using PHP?

Upvotes: 0

Views: 295

Answers (2)

webbiedave
webbiedave

Reputation: 48887

You didn't detail the field formats so here's a guestimate that you can tweak:

preg_match('/^{(\w+):(\d+){(\d+):(\w+)}}$/', '{an_option_name:123{456:a_variant_name}}', $matches);

$option_name  = $matches[1];
$option_id    = $matches[2];
$variant_id   = $matches[3];
$variant_name = $matches[4];

Upvotes: 1

matthewpavkov
matthewpavkov

Reputation: 2928

Why not use serialize() and unserialize()?

Upvotes: 7

Related Questions