Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP预匹配查找字段值_Php_Regex_Preg Match - Fatal编程技术网

PHP预匹配查找字段值

PHP预匹配查找字段值,php,regex,preg-match,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] 我尝试了几个不同的正则表达式,但没有

这是我的示例代码:

$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
的值

我试图在一个字符串中搜索多个字符串,并提取它们的值,以便以后更改

$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
)

这段代码做第一个输出请看-有帮助吗?很好!很高兴听到这个消息