PHP preg_match查找字段值

时间:2016-02-01 19:07:58

标签: php regex preg-match

这是我的示例代码:

$string = "teste aa=11 teste teste";
$var = "aa|bb|cc";
if (preg_match('/^'.$var.'/', $string,$matches)){
        echo "field [$matches[0]], value [$matches[1]]";

输出:

field [aa],value []

我试图输出如下:

field [aa],value[11]

我尝试了几种不同的正则表达式。如何使用正则表达式获取aa的值?

我试图在字符串中搜索多个字符串并选择它们的值以便以后更改它。

1 个答案:

答案 0 :(得分:0)

$string = "teste aa=11 teste teste";
$var = "aa|bb|cc";
preg_match('/^.*('.$var.')=(.*?)\s.*/', $string, $matches);
print_r($matches);

输出:

Array
(
    [0] => teste aa=11 teste teste
    [1] => aa
    [2] => 11
)