当前位置:Gxlcms > PHP教程 > 为何匹配不全求原因('/$(w+)=(.+?)$/i',$d,$matches2)

为何匹配不全求原因('/$(w+)=(.+?)$/i',$d,$matches2)

时间:2021-07-01 10:21:17 帮助过:40人阅读

$d = ‘$Row=10 $cid=1 $table=channel $sql=select * form abc $Where=[FatherID]=0 $’


preg_match_all('/\$(\w+)=(.+?) \$/i', $d, $matches2);
if($matches2){
$d2 = '';
for($k=0;$k $d2 = $d2 . $matches2[1][$k] . '="' .$matches2[2][$k] .'" ';
}
}

结果 只有 Row="10" table="channel" Where="[FatherID]=0"

应该是 Row="10" cid="1" table="channel" $sql="select * form abc" Where="[FatherID]=0" 才对呀 错在哪呢?


回复讨论(解决方案)

应该匹配到5个才对呀 为啥只有3个呢 求大神给个解决方案

$d = '$Row=10 $cid=1 $table=channel $sql=select * form abc $Where=[FatherID]=0 $';preg_match_all('/\$(\w+)=([^$]+)/i', $d, $matches2);print_r($matches2);
Array(    [0] => Array        (            [0] => $Row=10             [1] => $cid=1             [2] => $table=channel             [3] => $sql=select * form abc             [4] => $Where=[FatherID]=0         )    [1] => Array        (            [0] => Row            [1] => cid            [2] => table            [3] => sql            [4] => Where        )    [2] => Array        (            [0] => 10             [1] => 1             [2] => channel             [3] => select * form abc             [4] => [FatherID]=0         ))

你 /\$(\w+)=(.+?) \$/ 这样写不行的原因是
$Row=10 $cid=1 $table=channel $sql=select * form abc $Where=[FatherID]=0 $
看明白了吗?

用这个规则,你还需要 $d = str_replace('$', '$$', $d),

你 /\$(\w+)=(.+?) \$/ 这样写不行的原因是
$Row=10 $cid=1 $table=channel $sql=select * form abc $Where=[FatherID]=0 $
看明白了吗?

用这个规则,你还需要 $d = str_replace('$', '$$', $d),



啊 这么笨的正则啊。。。郁闷 嗯 我改进下 谢啦

你 /\$(\w+)=(.+?) \$/ 这样写不行的原因是
$Row=10 $cid=1 $table=channel $sql=select * form abc $Where=[FatherID]=0 $
看明白了吗?

用这个规则,你还需要 $d = str_replace('$', '$$', $d),



啊 这么笨的正则啊。。。郁闷 嗯 我改进下 谢啦

人气教程排行