【问题标题】:Notice: Undefined offset:注意:未定义的偏移量:
【发布时间】:2012-08-08 12:16:20
【问题描述】:

我完成了代码,它是这样的:

<?php
 ini_set('memory_limit', '128M');

    function validate_xml(&$filename)
    {
    libxml_use_internal_errors(true);
    $doc = new DOMDocument('1.0', 'utf-8');
    $xml = file_get_contents($filename); 
    $doc->loadXML($xml); 

    $errors = libxml_get_errors();


    $lines = file($filename);
    $output = fopen('errors.txt', 'a');

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

        }
    }

    fclose($output);

}


if(isset($_POST['SubmitCheck'])) {


        $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) {  
                        validate_xml($filename);                        
                        }  
              }
        }

        else {

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

它给了我

Notice: Undefined offset: 16032 in C:\xampp\htdocs\www\php-xml\nou.php on line 23

Notice: Undefined offset: 16051 in C:\xampp\htdocs\www\php-xml\nou.php on line 23

Notice: Undefined offset: 16060 in C:\xampp\htdocs\www\php-xml\nou.php on line 23

Notice: Undefined offset: 16075 in C:\xampp\htdocs\www\php-xml\nou.php on line 23

Notice: Undefined offset: 16078 in C:\xampp\htdocs\www\php-xml\nou.php on line 23

Notice: Undefined offset: 16101 in C:\xampp\htdocs\www\php-xml\nou.php on line 23

Notice: Undefined offset: 16110 in C:\xampp\htdocs\www\php-xml\nou.php on line 23

Notice: Undefined offset: 16167 in C:\xampp\htdocs\www\php-xml\nou.php on line 23

问题是我有 3 个大 xml(每个 11MB)。 当我在 3 个小 xml 上尝试脚本时,它起作用了。也为每个大 xml 单独工作。 我不知道该怎么做,我尝试增加 php_ini 的内存限制......仍然不知道发生了什么。

【问题讨论】:

  • fwrite($output, "文件名:{$filename}。在线错误:{$error->line}。{$lines[$error->line-1]}\n") ;

标签: php xml


【解决方案1】:

第 23 行的代码有一个无效的偏移量引用 (`{$lines[$error->line-1})。 你写道:

fwrite($output, "Filename: {$filename}.  Error on line: {$error->line}. {$lines[$error->line-1]}\n");

我想你想做:

$line_text = $lines[ $error->line - 1 ];
fwrite($output, "Filename: {$filename}.  Error on line: {$error->line}. $line_text \n");

按照您的做法,PHP 引擎将 $error-&gt;line-1 解释为好像 $line-1$error 的属性,而不是算术运算“$line 减 1”。

【讨论】:

  • 好吧,如果如您所说,该脚本将无法在单个文件上运行。我从 3 个 xmls (32Mb) 中制作了一个大 xml,该脚本适用于它,以及 3 个较小的 xmls
  • 请确认$lines数组的长度(例如echo count($lines); )。
  • 文件名:1.xml。在线错误:35100。... 这是为连接的 xml 完成的输出 errors.txt 的一行。代码运行良好,$lines 大约有 40k 行。
猜你喜欢
  • 2015-08-01
  • 2023-03-27
  • 2012-02-08
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
相关资源
最近更新 更多