Adding ()
s to mark groups should work, or if you want to make it more meaningful you can try named groups with (?<name>)
:
preg_match('/user.php?id=([\d]+)\\"/', $to, $matches);
// ^ ^
echo $matches[1]; // capture groups indexed from 1, 0 index is for the whole matched string
See the docs for more on the pattern sytnax.