I have a textfile, consisting of the following, and a couple of other things, but i've ommitted them here
###Sprint-Name###
SPRINT 20180403
###DEV-Number###
DEV-1039
###Functionality###
To do a thing
that document was uploaded via a form
and my code is as follows:
<?php
//Import the errorhandler
include_once('errorHandler.php');
//First of all, we need to get the contents of the test document so that we can process it
//The test document needs to be exploded based on newline
$testDocument = explode(PHP_EOL, file_get_contents($_FILES['testDocument']['tmp_name']));
echo '<pre>';
var_dump(array_search("###Sprint-Name###", $testDocument));
print_r($_POST);
print_r($_FILES);
print_r($testDocument);
echo '</pre>';
?>
but the var dump is returning bool(false)
, even though my $testDocument
array is:
Array
(
[0] => ###Sprint-Name###
[1] => QA Reporting
[2] => ###Dev-Number###
[3] => DEV-1231
[4] => ###Functionality###
[5] => To do a thing
)
if the array $testDocument is forced to be:
$testDocument = array("thing","###Sprint-Name###","other");
then that returns 1
, which is correct as the needle is at 1 in the haystack
Maybe it's to do with the encoding on the ###
, how can I check this, and how can I force it to a specific encoding? Thanks.
Edit: I tried removing the ###
from my strings and using different characters, just in case PHP thought they were comments or something, but this has no effect.
Your problem is splitting/exploding by PHP_EOL
.
If the files were created on a different system than the executing one, PHP_EOL
might not catch the right, or all new-line, carriage-return characters. This results in a value with a left over trailing in your case.
See here:
var_dump($testDocument[0]);
//output:
// string(18) "###Sprint-Name### "
// trailing character at the end
So, to solve that you could do a preg_slit
, with which you can split by several characters:
<?php
$input = <<<EOT
###Sprint-Name###
SPRINT 20180403
###DEV-Number###
DEV-1039
###Functionality###
To do a thing
EOT;
$testDocument = preg_split("/[
]+/", $input);
var_dump($testDocument);
$key = array_search("###Sprint-Name###", $testDocument);
var_dump($key); // int(0)
Or use file
instead of file_get_contents
, as @CBroe recommended.