<?php
$pattern ='/([^&]+)=([^=]+)(?=$|&[^=]+=)/';
$test = array(
'name=Shake & Bake&difficulty=easy',
'name=Shake&Bake&difficulty=easy',
'difficulty=easy&name=Shake & Bake',
'difficulty=easy&name=Shake&Bake',
'name=Shake&Bake',
'difficulty=easy',
'name=Shake&Bake&foo&difficulty=easy',
'name=Shake&Bake&difficulty=easy&',
'name=Shake&Bake&difficulty='
);
foreach($test as $foo) {
preg_match_all($pattern, $foo, $m);
echo $foo, "\n";
for($i=0; $i<count($m[0]); $i++) {
echo ' ', $m[1][$i], ' =$gt; "', $m[2][$i], "\"\n";
}
echo "\n";
}
?>
produces
name=Shake & Bake&difficulty=easy
name => "Shake & Bake"
difficulty => "easy"
name=Shake&Bake&difficulty=easy
name => "Shake&Bake"
difficulty => "easy"
difficulty=easy&name=Shake & Bake
difficulty => "easy"
name => "Shake & Bake"
difficulty=easy&name=Shake&Bake
difficulty => "easy"
name => "Shake&Bake"
name=Shake&Bake
name => "Shake&Bake"
difficulty=easy
difficulty => "easy"
name=Shake&Bake&foo&difficulty=easy
name => "Shake&Bake&foo"
difficulty => "easy"
name=Shake&Bake&difficulty=easy&
name => "Shake&Bake"
difficulty => "easy&"
name=Shake&Bake&difficulty=
name => "Shake&Bake"
which seems to be working (except for difficulty= not being matched in the last example).
I'm not sure whether a once-only subpattern matching would improve the speed. You might want to look this up.