1

我正在编写一个从 CSV 文件中读取 MAC 地址和位置的脚本。问题是,我使用了 preg_match_all 两次。第一个是找到MAC地址。第二个是基于写入位置的格式。我知道这两个正则表达式都有效,因为我已经切换了它们。

我试图得到这样的输出:
Mac Adress 0c:85:25:c7:ab:f0 is located in Tilburg-Compleet > P1 > P1-2e

但是,我现在收到的输出是:
Mac 地址0c:85:25:c7:ab:f0位于

请帮忙?

亲切的问候,布赖恩

<?php
if (($handle = fopen("AP_Summary_20121004_104813_198.csv", "r")) !== FALSE) {
  while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

    $num = count($data);
    for ($i=0; $i < $num; $i++) {

      $m = preg_match_all('~[0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}~', $data[$i], $match); 
      $c = preg_match_all('~^.{1,}\s\W\s.{1,}\s\W\s$~', $data[$i], $matches);

      if ($m) { 

        foreach ($match as $val){
          foreach ($matches as $values){
            echo 'The Mac Address <b>' . $val[0] . '</b> is located in <b>' . $values[0] . '</b><br /><br />';
          }
        }
      }

    }
  }
  fclose($handle);
}

?>

这是 CSV 文件的一小部分

EHV-R1-1e-109113   ,d8:24:bd:e9:ce:f0   ,Eindhoven-rachelsemolen-TUE-TF > R1 > R1-1e        ,Enabled   
EHV-R1-1e-109117   ,d8:24:bd:e9:e6:40   ,Eindhoven-rachelsemolen-TUE-TF > R1 > R1-1e   ,Enabled   
EHV-R1-1e-109118   ,d8:24:bd:e9:ed:b0   ,Eindhoven-rachelsemolen-TUE-TF > R1 > R1-1e   ,Enabled   
Ehv-R1-2e-27088   ,00:12:44:ba:1e:00   ,Eindhoven-rachelsemolen-TUE-TF > R1 > R1-2e   ,Enabled   
Ehv-R1-2e-27090   ,00:12:44:ba:1f:b0   ,Eindhoven-rachelsemolen-TUE-TF > R1 > R1-2e   ,Enabled   
Ehv-R1-2e-27086   ,00:12:44:ba:20:c0   ,Eindhoven-rachelsemolen-TUE-TF > R1 > R1-2e   ,Enabled   
Ehv-R1-2e-27087   ,00:12:44:ba:21:40   ,Eindhoven-rachelsemolen-TUE-TF > R1 > R1-2e   ,Enabled   
Ehv-R1-2e-27084   ,00:12:44:ba:2b:10   ,Eindhoven-rachelsemolen-TUE-TF > R1 > R1-2e   ,Enabled   
Ehv-R1-2e-27085   ,00:12:44:ba:32:a0   ,Eindhoven-rachelsemolen-TUE-TF > R1 > R1-2e   ,Enabled   
Ehv-R1-2e-27094   ,00:12:44:ba:77:10   ,Eindhoven-rachelsemolen-TUE-TF > R1 > R1-2e   ,Enabled   
Ehv-R1-2e-63664   ,00:1b:8f:89:28:c0   ,Eindhoven-rachelsemolen-TUE-TF > R1 > R1-2e   ,Enabled   
Ehv-R1-2e-100264   ,00:3a:98:62:ae:a0   ,Eindhoven-rachelsemolen-TUE-TF > R1 > R1-2e   ,Enabled   
Ehv-R1-2e-100266   ,00:3a:98:62:af:e0   ,Eindhoven-rachelsemolen-TUE-TF > R1 > R1-2e   ,Enabled   
Ehv-R1-2e-100263   ,00:3a:98:72:b6:40   ,Eindhoven-rachelsemolen-TUE-TF > R1 > R1-2e   ,Enabled   
Ehv-R1-2e-100265   ,00:3a:98:72:b9:f0   ,Eindhoven-rachelsemolen-TUE-TF > R1 > R1-2e   ,Enabled   
Ehv-R1-2e-100261   ,00:3a:98:72:ba:f0   ,Eindhoven-rachelsemolen-TUE-TF > R1 > R1-2e   ,Enabled   
4

1 回答 1

0

鉴于正则表达式是正确的(由于您没有为输入提供示例,因此无法告知),您可以使用以下方法获得所需的结果:

if ($m) { 
    foreach ($match as $valKey => $val){
        echo 'The Mac Address <b>' . $val[0] . '</b> is located in <b>' . $matches[$valKey][0] . '</b><br /><br />';
    }
}

编辑:但这只有在结果对称的情况下才是正确的。而且,在那种情况下(正如 Fabrício Matté 指出的那样),您应该能够处理单个 preg_match_all 并用括号捕获您想要的输入,即

preg_match_all('~([0-9])sometext([a-f])~', $data[$i], $match);

所以$match[1]会给你该([0-9])部分中的任何内容,并$match[2]会给你正([a-f])则表达式中的内容。

EDIT2:根据您提供的数据,您应该与以下内容相处融洽:

preg_match('~^.*?([0-9a-f:]{17}).*?,(.*?),~i', $data[$i], $matches);
echo "MAC :".$matches[1];
echo "Location :".trim($matches[2]);

你明白了,当然你可以像你一样限制正则表达式,但关键是你可以得到正则表达式中匹配的部分,这些部分在$matches数组中的括号中,括号中组的位置的升序索引在你的正则表达式中从左到右。

于 2013-05-26T21:54:31.313 回答