0

Let's say I have a line in a php file:

$config['fw_ls'] = 'some value';

I want to get the values between the quotes. What I tried are combinations like:

preg_match('/\$config\[\'fw_ls\'\] = \'([^\'])*\';/',$sData,$aK);
var_dump($aK);

Sadly I can't seem to get the value between the quotes, in above example it returns the character 't'. I really don't understand what I'm doing wrong. I'd expect the regex would search starting from the quote and get everything until it finds another quote. But that doesn't seem to be quite right.

Any help would be appreciated.

4

2 回答 2

0

您可能想使用解析器,然后获取诸如数组索引值之类的东西。这是一个开源 php 解析器的示例(在 PHP 中):

https://github.com/nikic/PHP-Parser

正则表达式不是解析器,因此很难根据您的问题判断您是否尝试解析任意 php 行,或者这是正则表达式可能成为您朋友的特殊情况。

于 2013-08-19T18:47:55.400 回答
0

尝试这个:

$text = "\$config['fw_ls'] = 'some value';";
preg_match("#\\\$config\['fw_ls'\]\s*=\s*'([^']+)';#", $text, $match);
print_r($match);

Array
(
    [0] => $config['fw_ls'] = 'some value';
    [1] => some value
)
于 2013-08-19T18:51:15.313 回答