合并 2 个 php 脚本


Merge 2 php scripts

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

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

谢谢

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