1

Below is the print_r version of my array ($hamle). As you can see index(80) is empty or there is a end of line or a similar character there. I tried many options to delete it if it exists and is empty but couldn't do it. I strongly think that it is reading an empty line during the 'read while' loop. It doesn't appear in all files. It appears sometimes. Can anyone think of a chic way out?

Array ( [0] => 1.b4 [1] => d5 [2] => 2.Bb2 [3] => Qd6 [4] => 3.a3 [5] => e5 [6] => 4.e3 [7] => a5 [8] => 5.b5 [9] => Nf6 [10] => 6.c4 [11] => Bg4 [12] => 7.Be2 [13] => Bxe2 [14] => 8.Qxe2 [15] => Nbd7 [16] => 9.d4 [17] => dxc4 [18] => 10.Nf3 [19] => e4 [20] => 11.Ne5 [21] => Nb6 [22] => 12.Nd2 [23] => Qe6 [24] => 13.O-O [25] => Bd6 [26] => 14.Nexc4 [27] => Nxc4 [28] => 15.Nxc4 [29] => O-O [30] => 16.Rfc1 [31] => Be7 [32] => 17.a4 [33] => Nd5 [34] => 18.Ba3 [35] => Bb4 [36] => 19.Qb2 [37] => Rfc8 [38] => 20.Bxb4 [39] => axb4 [40] => 21.Nd2 [41] => c6 [42] => 22.b6 [43] => Qe7 [44] => 23.Rc5 [45] => Ra6 [46] => 24.a5 [47] => g6 [48] => 25.Nb3 [49] => Rca8 [50] => 26.Rac1 [51] => Kg7 [52] => 27.Qd2 [53] => Qd6 [54] => 28.R1c4 [55] => Rd8 [56] => 29.Rxb4 [57] => Nxb4 [58] => 30.Qxb4 [59] => Qe7 [60] => 31.h3 [61] => Rd5 [62] => 32.Kf1 [63] => Ra8 [64] => 33.Qa4 [65] => Qd6 [66] => 34.Ke2 [67] => Kf8 [68] => 35.Rxd5 [69] => cxd5 [70] => 36.Nc5 [71] => Ke7 [72] => 37.Qb5 [73] => Rb8 [74] => 38.Kd2 [75] => f5 [76] => 39.Kc3 [77] => g5 [78] => 40.Kb4 [79] => 1-0 [80] => )

The code =>

while(! feof($file))
  {


 $n=$n+1;
            $hamle1= fgets($file);
    $hamle1 = str_replace("\n", "", $hamle1);
    $hamle1 = str_replace("\r", "", $hamle1);
    $hamle1 = trim($hamle1);
    $hamle1 = explode(" ", $hamle1);

        foreach($hamle1 as $item)
    {
        $hamle[] = $item;
    }
5
  • 1
    Please post your code. Also, prettifying your array would also help. Commented May 10, 2016 at 2:50
  • 1
    @Dagon... Thank you I will try and let you know... Regards Commented May 10, 2016 at 2:57
  • 1
    Hello @Dagon The empty element is still there... Regards Commented May 10, 2016 at 3:04
  • 2
    var_dump($item), possibly even var_dump(bin2hex($item)) – what exactly does "empty" mean here? Commented May 10, 2016 at 3:06
  • 2
    If you are using trim function then no need to use str_replace("\n", "", $hamle1); str_replace("\r", "", $hamle1) Commented May 10, 2016 at 3:21

2 Answers 2

4

array_filter will remove empty entries

$hamle = array_filter($hamle);
Sign up to request clarification or add additional context in comments.

1 Comment

...and any entries that seem empty like 0, false, etc.
1

To delete the last element of array if it's empty then use below code.

// get the last element of array
if(trim(end($hamle)) == '')
{
   // Remove last element from array
   array_pop($hamle) ;
}

Kindly visit below links to understand the functions which are used in above code.

trim

end

array_pop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.