I have a function which opens a remote file to get its content with the cURL library. Then the function returns an array containing the content of the file.
Then, when it checks whether that specific value exists in the array by using the in_array
function, it always shows that the value doesn't exist, even though it does.
Here's the code and also the content of remote file.
function getCountry($file) {
$fop = curl_init($file);
curl_setopt($fop, CURLOPT_HEADER, 0);
curl_setopt($fop, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($fop);
curl_close($fop);
$fcontent = explode("\n", $result);
return $fcontent;
}
$file = "http://localhost/countries.txt";
$countries = getCountry($file);
if (in_array('italy', $countries)) {
echo "Exists";
} else {
echo "Not exists";
}
In the content of the remote file countries.txt
, every sentence or word in a line is like this:
spain
italy
norway
canada
france
As I mentioned previously, it always shows that the value doesn't exist, even though it does.
\n
with\r\n
.\r\n
only moves the problem, what if a future user of your program uses another editor that only inserts LFs instead of CRLFs? Never get stuck on programming for a specific case, fix generically, for example with$fcontent = array_map('trim', explode("\n", $result));