要解析的字符串:
$str = "
public $xxxx123;
private $_priv ;
$xxx = 'test';
private $arr_123 = array();
"; // | |
// ^^^^^^^---- get the variable name
到目前为止我得到了什么:
$str = preg_match_all('/\$\S+(;|[[:space:]])/', $str, $matches);
foreach ($matches[0] as $match) {
$match = str_replace('$', '', $match);
$match = str_replace(';', '', $match);
}
它有效,但我想知道我是否可以改进 preg,例如摆脱这两个str_replace
并可能包含\t
在(;|[[:space:]])