Regular Expression for variable names

So I needed to grab all variables from a script to do various things with, and spent a while tracking down the regular expression to grab valid variable names. The official page on Regular Expression for variable names states ‘A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.’, which is purely the labeling rules for everything after the $.

As well as this we need to also tell it to look for the $ itself, and to output an array of the variables and just the variable names.

http://docs.php.net/manual/en/language.variables.basics.php

$string = '$Lorem ipsum dolor sit $amet, consectetur '$adipiscing' elit. Vestibulum nulla magna, tristique sit amet tempor ornare, iaculis ut sapien.';
preg_match_all('/[\$]([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/',$
string,$matches);
print_r($matches);

This should output:
Array ( [0] => Array ( [0] => $Lorem [1] => $amet [2] => $adipiscing ) [1] => Array ( [0] => Lorem [1] => amet [2] => adipiscing ) )