// Вот регулярка вашей мечты:
$pattern = '/^(?<prefix>\w+)|(?<names>\w+),(?<values>\w+)/i';
$value = 'string|name1,value1|name2,value2';
preg_match_all($pattern, $value, $matches);
var_dump($matches);
// Можно почистить лишние матчеры
array_walk($matches, function(&$item, $key) use (&$matches) {
if (is_numeric($key)) {
unset($matches[$key]);
return;
}
foreach ($item as $key => $match) {
if (empty($match)) {
unset($item[$key]);
}
}
});
var_dump($matches);
<?php
$s = 'string|name1,value1|name2,value2|name3,value3';
$b = array();
foreach( explode('|', $s) AS $e ) {
list($k, $v) = explode(',', $e);
$b[$k] = $v;
}
?>
$b: Array
(
[string] =>
[name1] => value1
[name2] => value2
[name3] => value3
)
preg_match_all("~([^|,]+)\|([^,]+)~","string|name1,value1|name2,value2|name3",$m);var_dump($m);
[1]=>
array(3) {
[0]=>
string(6) "string"
[1]=>
string(6) "value1"
[2]=>
string(6) "value2"
}
[2]=>
array(3) {
[0]=>
string(5) "name1"
[1]=>
string(5) "name2"
[2]=>
string(5) "name3"
}