0

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.

4
  • 1
    Try replacing \n with \r\n. Commented May 3, 2013 at 17:06
  • Nice solution and easy too (+1). Commented May 3, 2013 at 17:13
  • Thanks! I think that it would be better to use regex though. Check out my answer. Commented May 3, 2013 at 17:14
  • Replacing it with \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)); Commented May 3, 2013 at 17:24

2 Answers 2

2

I'm mighty sure you've got sparse characters such as carriage returns in your source file. Try this after the getCountry call:

foreach($countries as &$country) {
  echo "'$country' (".strlen($country).")<br>";
  $country = trim($country);
}

Wouldn't surprise me if it gave a strlen of 6 for 'italy', and fix the problem along the way.

The proper fix would be to clean up the content right after parsing:

$fcontent = array_map('trim', explode("\n", $result));
8
  • +1, this is exactly what I thought when he said he is able to retrieve the last line! Commented May 3, 2013 at 17:07
  • very nice, would a better step be removing all characters that aren't A to Z? Commented May 3, 2013 at 17:08
  • @DavidChen depends entirely on the input file, in this case I'm just suggesting to clean up the cr/lf mess. Commented May 3, 2013 at 17:09
  • mmhm, I was thinking that when I suggested \r\n, considering that the only characters possible for a country name is A-Z (English), I think it might be better to use some regular expression. Commented May 3, 2013 at 17:11
  • That attitude might get you into a fight with people from South Africa, The Bahamas, Бо�?на и Херцеговина or Côte d'Ivoire. Commented May 3, 2013 at 17:13
1

If you're not sure whether there will be CRLF in the file, instead of explode() you can use preg_split() like this:

return preg_split('/\r?\n/', $result);

Alternatively, apply trim() to each result:

return array_map('trim', explode("\n", $result));

The latter will also remove leading and trailing spaces and tabs which may not always be suitable.

1
  • Lol, you added the array_map addition simultaneously with me hehe. Commented May 3, 2013 at 17:26

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.