【发布时间】:2014-07-02 10:00:11
【问题描述】:
我有这样的字符串:
public $host = 'sth'; public $user = 'sth'; public $password = 'sth';public $db = 'dbname'; public $dbprefix = 'sth';
我想动态获取 $db 值。我试过正则表达式:
#(.*)public(.*)$db(.*)=(.*)\'(.*)\';#is
空数组结果
请不要写我可以获取类和属性的实例,因为我不能,仅此而已。有比正则表达式更可行的方法。
问题解决了!
$file="public $host = 'sth'; public $user = 'sth'; public $password = 'sth'; public $db = 'get this value';
public $dbprefix = 'sth';";
$regex = "/.*public\s+$db\s+=\s+'(.*?)';/i";
preg_match($regex,$file,$matches);
echo '<br><pre>';var_dump($matches);
$matches[1] 是'获取这个值';
【问题讨论】:
-
你为什么要捕获你所有的
.*s?