Can I avoid the empty array that generate by foreach
?
here is my code
$file="test.txt";
$open = file_get_contents($file);
$lines = explode(PHP_EOL, $open);
foreach($lines as $line){
$domain = preg_split('/\s+/', $line,-1,PREG_SPLIT_NO_EMPTY);
echo "<pre>";
var_dump($domain);
echo "$domain[0] OK";
echo "</pre>";
}
test.txt contains this
www.google.com 2.2.2.3
www.test.com 2.2.2.3
www.example.com 2.2.2.3
after I ran my script the result is this
array(2) {
[0]=>
string(14) "www.google.com"
[1]=>
string(7) "2.2.2.3"
}
www.google.com OK
array(2) {
[0]=>
string(12) "www.test.com"
[1]=>
string(7) "2.2.2.3"
}
www.test.com OK
array(2) {
[0]=>
string(15) "www.example.com"
[1]=>
string(7) "2.2.2.3"
}
www.example.com OK
array(0) {
}
OK
What cause the array(0)
to appear?
There's no new line after www.example.com or other element.
Can I remove it or simply don't get it generated?