7

PHP is always returning false when I use in_array(), and whether it is or isn't in the array doesn't matter. For example:

$list = 'test
list
example';
$list_arr = array();
$list_arr = explode("\n",$list);
if (in_array('test',$list_arr)) {
    echo 'Found';
}
else {
    echo 'Not Found';
}

Returns 'Not Found' even though 'test' is a value in the array

$list = 'test
list
example';
$list_arr = array();
$list_arr = explode("\n",$list);
if (in_array('qwerty',$list_arr)) {
    echo 'Found';
}
else {
    echo 'Not Found';
}

Returns 'Not Found', as it should

Why is it returning false when it should be true? Please give any suggestions. The list I am actually using has more than 10K of values and therefore I just shortened it here.

3
  • What OS are you using? In windows, an end-of-line is actually two characters: \r\n, so splitting it on \n will give you 'text\r', 'list\r' and 'example'. Commented Jul 16, 2012 at 20:26
  • are you sure you have a \n and not a \r\n? Commented Jul 16, 2012 at 20:26
  • Can you var_dump($list_arr); and post the output? Commented Jul 16, 2012 at 20:32

4 Answers 4

11

If you are using this exact example and are coding on a Windows PC, the problem comes from the fact that Windows stores line feeds using:

\r\n

Thus, when you split on \n only you keep an invisible \r at the end of your items and they don't match for that reason.

Sign up to request clarification or add additional context in comments.

9 Comments

I am on linux, however that did fix the problem
It can be configured in the editor, which should not be notepad
I also use Windows to code it, it is just running on a linux server. Could that be why?
I am confused, didn't you just say you are using linux? If you use notepad++, configure it to save newlines as '\n' (UNIX). See stackoverflow.com/questions/8195839/…
As Esailija said, the source of your file has to be coded in a Linux format if you want to use PHP_EOL on linux. But if your server runs on Windows, then make sure your NotePad++ is configured for Windows style line endings.
|
8

Your list is actually separated by \r\n, not just \n.

$list_arr = explode("\r\n",$list);

6 Comments

Beat me to it by what... 4 seconds?
Right, and when you separate by "\r" you end up with "text\r" as the string - so try putting that in the in_array() just to see.
@MathieuDumoulin: I'm a ninja :P
No you're a @Rocket, much faster!
@MathieuDumoulin: Rocket Ninja.
|
1

That is a pretty odd way of getting values into a list. So I am assuming that you are ultimately trying to lines from a separate file into an array. In which case you would just do

file('path/to/file');

To read each line into a separate array item.

Where the file referenced looks like:

test
list
example

If you know the values up front like in your code sample, you should just put them into an array rather than reading them from a string.

If you simply must read the values from a string, I would suggest using PHP_EOL constant to define your line breaks. Like this:

$list = 'test' . PHP_EOL . 'list' . PHP_EOL . 'example';
$list_arr = explode(PHP_EOL, $list);

The PHP_EOL constant is going to give you a much better platform-independent way of getting values defining your line breaks.

Comments

1

As already stated, Windows uses \r\n as end of line characters. Unix uses \n and Mac uses \r. To make portable code, use the PHP_EOL constant to explode your string:

$list_arr = explode(PHP_EOL, $list)

That way, you are sure it always works correctly.

2 Comments

Not true, as Windows will store EOL as \r\n but if you run it on a linux pc, it will be \n only: ca3.php.net/manual/en/reserved.constants.php
@MathieuDumoulin the provided link does not state that. It just says "the right end-of-line character" for the platform. My solution wouldn't work if you'd use a Windows terminated editor on a Unix platform.

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.