I am using the following code to match all variables in a script starting with '$', however i would like the results to not contain duplicates, ie be distinct/unique:
preg_match_all('/\$[a-zA-Z0-9]+/', $code, $variables);
Any advice?
I am using the following code to match all variables in a script starting with '$', however i would like the results to not contain duplicates, ie be distinct/unique:
preg_match_all('/\$[a-zA-Z0-9]+/', $code, $variables);
Any advice?
Use array_unique
to remove the duplicates from your output array:
preg_match_all('/\$[a-zA-Z0-9]+/', $code, $variables);
$variables = array_unique($variables[0]);
But I hope you’re not trying to parse PHP with that. Use token_get_all
to get the tokens of the given PHP code.