2016-01-13 26 views
0

我想在其他字符串中找到一個字符串。我不允許使用任何str函數。因此,我寫了這段代碼!但它顯示了第18行Undefined offset: 14 in C:\nn\htdocs\myfiles\PHPCode.php。 我不知道這條線有什麼問題。我geting注意:未定義偏移量:14在C: nn htdocs myfiles PHPCode.php在線18

if ($arr1[$j] == $arr2[$index]) 

完整的代碼

<?php 
     $stringR = strtolower($_POST['name']); 
     $first = strtolower($_POST['First']); 
     $k = 0; 
     $index =0; 
     $firstfeq =0; 
     if (preg_match('/[0-9]/', $stringR)){ 
      echo "ERROR:The String has numbers"."<br>"; 
      exit; 
     } 
     if ((strlen($first) > 8)){ 
      echo "ERROR: Number of char in each input should be less  than 8 char"."<br>"; 
      exit; 
     } 
     $arr1 = str_split($stringR); 
     $arr2 = str_split($first); 
     for ($j = 0 ; $j <= sizeof($arr1) ; $j++) { 
      if ($arr1[$j] == $arr2[$index]){ 
       if ($index <= (sizeof ($arr2))){ 
        $k = 1; 
        $index++; 
       }else 
       { 
        $index = 0; 
        $k =0; 
       } 
       if (($index == sizeof($arr2)) && ($k == 1)) { 
        $firstfeq++; 
        $index =0; 
        $k = 0; 
       } 

      } 

     } 
     echo "$first appears $firstfeq"."<br>"; 

?> 
+0

'$ arr2'數組中不存在'$ index' 14 ...錯誤說明了這一切。 – Darren

回答

1

更改此:

for ($j = 0 ; $j <= sizeof($arr1) ; $j++) { 

這樣:

for ($j = 0 ; $j < sizeof($arr1) ; $j++) { 

我下注$ j被持續1個指數的總限額以上。

1

因爲數組索引爲0,應更換此行

for ($j = 0 ; $j <= sizeof($arr1) ; $j++) { 

for ($j = 0 ; $j < sizeof($arr1) ; $j++) { 

,同樣

if ($index <= (sizeof ($arr2))){ 

if ($index < (sizeof ($arr2))){ 
相關問題