2012-08-08 100 views
0

我需要一些PHP腳本的幫助。我編寫了一個腳本來分析xml並報告txt文件中的錯誤行。合併2個PHP腳本

代碼是這樣的。

<?php 


    function print_array($aArray) 
    { 
     echo '<pre>'; 
     print_r($aArray); 
     echo '</pre>'; 
    } 

    libxml_use_internal_errors(true); 
    $doc = new DOMDocument('1.0', 'utf-8'); 
    $xml = file_get_contents('file.xml'); 
    $doc->loadXML($xml); 

    $errors = libxml_get_errors(); 
    print_array($errors); 

    $lines = file('file.xml'); 
    $output = fopen('errors.txt', 'w'); 

    $distinctErrors = array(); 
    foreach ($errors as $error) 
    { 
     if (!array_key_exists($error->line, $distinctErrors)) 
     { 
      $distinctErrors[$error->line] = $error->message; 
      fwrite($output, "Error on line #{$error->line} {$lines[$error->line-1]}\n"); 

     } 
    } 

    fclose($output); 

?> 

打印陣列只能看到錯誤,它的唯一可選。 現在我的僱主發現了一段代碼在網上

<?php 


// test if the form has been submitted 
if(isset($_POST['SubmitCheck'])) { 
    // The form has been submited 
    // Check the values! 

     $directory = $_POST['Path']; 

     if (! is_dir($directory)) { 
       exit('Invalid diretory path'); 
       } 
       else 
       { 
        echo "The dir is: $directory". '<br />'; 
         chdir($directory); 

         foreach (glob("*.xml") as $filename) { 
         echo $filename."<br />"; 
         } 
       } 
     } 

     else { 
    // The form has not been posted 
    // Show the form 
     ?> 
     <form id="Form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
      Path: <input type="text" name="Path"><br> 
      <input type="hidden" name="SubmitCheck" value="sent"> 
      <input type="Submit" name="Form1_Submit" value="Path"> 
     </form> 
     <?php 
     } 
?> 

,基本上發現在一個給定的目錄中的所有XML,並告訴我,到2個腳本合併。 我給了輸入目錄,腳本應該在該目錄中的所有xmls上運行,並在txt文件中給出報告。

我不知道該怎麼做,我是一個PHP初學者,花了我大約2-3天的時間寫出最簡單的腳本。有人可以幫我解決這個問題嗎?

感謝

回答

0

做一個功能AOUT的代碼,並替換所有「file.xml」的參數如$文件名。 在「echo $ filename」所在的第二個腳本中,調用您的函數。

+0

謝謝,我終於做到了。謝謝一堆 – Mickey 2012-08-08 10:49:55