PHP - 多次使用preg_match_all

时间:2013-05-26 21:43:01

标签: php csv preg-match-all

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

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

然而,我现在收到的输出是:
Mac Adress 0c:85:25:c7:ab:f0 位于

请帮帮忙?

亲切的问候,Brian

<?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   

1 个答案:

答案 0 :(得分: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ícioMatté指出的那样),你应该能够与一个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数组的括号中从左到右的正则表达式中括号中组的位置。