我将产品选项作为字符串保存在数据库中,我需要解析它(字符串到数组)。
字符串是:
{option_name:option_id{variant_id:variant_name}}
如何使用 PHP 解析它?
为什么不使用serialize()
and unserialize()
?
您没有详细说明字段格式,所以这是您可以调整的猜测:
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];