Reputation: 55
Straight up: No, it's not the "usual question" and yes - I've read the other StackOverflow articles regarding this topic and they didn't help me.
If you create the array within a file and look at the output:
$names = ["test123", "test!?", "test"];
var_dump($names);
Output:
array(3) {
[0]=>
string(7) "test123"
[1]=>
string(6) "test!?"
[2]=>
string(4) "test"
}
... which is completely correct. 3 elements each a string.
BUT if you read the lines of a file via explode and input them into an array:
$names = explode("\n", file_get_contents('list.txt'));
var_dump($names);
Output:
array(3) {
[0]=>
" string(8) "test123
[1]=>
" string(7) "test!?
[2]=>
string(4) "test"
}
... which differs from the first one as these are all strings, but (excluding the last one) having one character more each + not being quoted as the array above.
So my question: How can I read all the lines of a file into elements of an array, with the exact spelling of the first example I've delivered? I need the exact format as a "normal array" for the further programme, but I can't get it working :)
Upvotes: 0
Views: 50
Reputation:
i think you can use this line of code
$lines = file($filename, FILE_IGNORE_NEW_LINES);
Upvotes: 2