2013-05-22 85 views
2

我需要從here創建一個數據庫表:讀一個大文本文件,並選擇行復制到另一個文件

所以,我使用curl命令複製相同的文件到服務器的文件夾。我需要從上面的文本文件中檢索00-00-00 (hex) XEROX CORPORATION, 00-00-01 (hex) XEROX CORPORATION等。我只需要將十六進制值和組織的第一行復制到另一個文本文件。

我該怎麼用PHP做到這一點?

回答

1
<?php 
    // open the input and the output 
    $fp = fopen("http://standards.ieee.org/develop/regauth/oui/oui.txt","r"); 
    $out = fopen("somefile.txt","w"); 

    while($rec = fgets($fp)){ 
     // check to see if the line contains '(hex)' 
     if (strpos($rec, '(hex)') !== false){ 
      // if so write it out 
      fputs($out, $rec."\n");    
     } 
    } 
    fclose($fp); 
    fclose($out); 
1

試試這個

<?php 
     $i   = 1; 
     $a_line  = ""; 
     $first_line = ""; 
     $handle  = @fopen("/tmp/inputfile.txt", "r"); 

     if ($handle) { 
      while (($buffer = fgets($handle, 4096)) !== false) { 

       if($i == 1) 
        // here you have first line 
        $first_line = $buffer; 
       else { 

        // check the $buffer contains the substring (hex) and XEROX CORPORATION 
        // if it contains put it in another variable 

        $a_line .= $buffer; 

       } 



      } 
      if (!feof($handle)) { 
       echo "Error: unexpected fgets() fail\n"; 
      } 
      fclose($handle); 
     } 
     // finally write the $first_line to a header file 
     // then write $a_line to another file 

    ?> 
0
<?php 
error_reporting(0); 
$fs=fopen("1.txt", "r"); 
$ft=fopen("2.txt", "w"); 

if ($fs==NULL) 
{ 
    echo "Can't Open Source File ..."; 
    exit(0); 
} 
if ($ft==NULL) 
{ 
    echo "Can't Open Destination File ..."; 
    fclose ($fs); 
    exit(1); 
} 

else 
{ 
    while ($ch=fgets($fs)) 
     fputs($ft, $ch); 

    fclose ($fs); 
    fclose ($ft); 
} 

?>

相關問題