0

我需要一些关于 php 脚本的帮助。我编写了一个脚本来解析 xml 并在 txt 文件中报告错误行。

代码是这样的。

<?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,并告诉我合并这两个脚本。我给出了输入目录,脚本应该在该目录中的所有 xml 上运行,并在 txt 文件中给出报告。

而且我不知道该怎么做,我是 PHP 的初学者,花了我大约 2-3 天的时间来编写最简单的脚本。有人可以帮我解决这个问题吗?

谢谢

4

1 回答 1

0

从您的代码中创建一个函数并将所有“file.xml”替换为一个参数,例如$filename。在“echo $filename”所在的第二个脚本中,调用您的函数。

于 2012-08-08T09:39:14.187 回答