【问题标题】:error in php programming [duplicate]php编程中的错误[重复]
【发布时间】:2014-02-24 10:50:31
【问题描述】:

我有一个文本文件 write.txt:-write.txt

My name

jai hind

stack

我在以下代码的第 154 行和第 165 行收到错误,错误是:-

PHP 注意:未初始化的字符串偏移量:/home/sakshi/Desktop/syllabify/step2/Syll_script_1.php 第 154 行中的 11

PHP 注意:未定义的偏移量:/home/sakshi/Desktop/syllabify/step2/Syll_script_1.php 第 165 行中的 -2

代码:-

$file1 = fopen("write.txt", "r");

$q=1;
while ( $line = fgets($file1) )
{

    $i=0;
    $arr = explode( "\n", $line );
    $q=$q+1;
    if(strcmp($arr[0],"." ) == 0)
    {
        $i=1;
        $q=$q-1;
        echo "\nposition of dot in write.txt:";
        echo "$i \t $q\n";
        break;
    }
}
fclose($file1);

if($i==0)
    echo $i;

while ( $q!=1 )
{

    $file_input=fopen("input.txt","r");
    $line = fgets($file_input);
    $p = strlen($line);
    echo $p;
    $a=2;
    if ($p == $a)
    {
        $m = $line[0];
        echo "$m\n";

        $l=0; $h=0;
        $filename=fopen("write.txt","r");
        $content=file("write.txt");

        for($i=0;$i<$u-1;$i++)
        {
            $arr=$content[$i];
            $length=strlen($arr); 

            for($j=0;$j<=$length;$j++)
            {
                if(strcmp($arr[$j],$m ) == 0)// line 154
                {
                    echo "\nYES PRESENT AT LINE NUMBER: ";
                    $h=$i+1;
                    echo "$h\n";
                    $l=$h+1;
                }
            }
            break;
        }
        /********************************************************/             
        $arr3=$content[$l-2];// line 165
        $ar3=explode(" ",$arr3);
        $output_file=fopen("Result.txt","a");
        fwrite($output_file,$ar3[0]);
        fwrite($output_file," ");
        fclose($output_file);     
        $output_file=fopen("Result.txt","a");
        fwrite($output_file,$ar3[1]);
        fwrite($output_file," ");
        fclose($output_file);
        $ar3=str_replace("\n","",$ar3);
        $output_file=fopen("Result.txt","a");
        fwrite($output_file,$ar3[2]);
        fclose($output_file);
        /************************************************************/
        // put enter at the end of operation
        $output_file=fopen("Result.txt","a");
        fwrite($output_file,"\n");
        fclose($output_file);
        /**********************************************************/


    }

【问题讨论】:

  • 你能发布你的完整代码吗?
  • 我复制了代码,加了两个'}',没有报错。
  • 什么是$u?这不是完整的代码。
  • 你初始化$m了吗?
  • 你想要什么准确的输出,这样我们会更好地理解..

标签: php string file


【解决方案1】:

问题出在下面一行:

for($j = 0; $j <= $length; $j++)

应该是

for($j = 0; $j < $length; $j++)

没有“=”符号。因为等号的迭代将超过字符串的长度。从 0 开始计数,直到 strlen()-1。因此

$j < strlen(); 

否则你应该申请

$j <= strlen() - 1;

【讨论】:

    【解决方案2】:

    在访问元素之前检查 $arr 是否是一个数组。

    【讨论】:

    • $arr 是一个已分配字符串的变量。但我正在尝试从 $arr 访问每个字符
    • 还要检查是否不为空
    猜你喜欢
    • 2011-08-22
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 2012-05-29
    • 1970-01-01
    • 2013-07-13
    相关资源
    最近更新 更多