Jump to content


Photo

in_array() problem


  • Please log in to reply
7 replies to this topic

#1 Hologram_

Hologram_
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 20 July 2006 - 08:55 AM

hi all,
this is my code
$string = "string";
$theFile = file_get_contents('file.txt');
$lines = array();
$lines = explode("\n", $theFile);
$lineCount = count ($lines);

if (in_array ($string, $lines)) {
  echo "in";
}
my problem is that in_array() returns false also when string is in array
have no idea how to solve this

thanks in advance

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 20 July 2006 - 09:04 AM

First off, a tip:
<?php
$theFile = file_get_contents('file.txt');
$lines = array();
$lines = explode("\n", $theFile);

//The above can be replaced with just:
$lines = file("file.txt");
?>
Maybe you could give us an extract of file.txt? You are aware that for there to be a match, "string" has to be sitting on a line of its own, right? Not in the middle of a larger string...

#3 Hologram_

Hologram_
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 20 July 2006 - 09:27 AM

Hi SemiApocalyptic,

file contains lines like this:
blue
orange
yellow
green
I can find the string (e.g. "blue") in $theFile, but not in array $lines

tried your code, thanks
$lines = file("file.txt");
print_r ($lines);
if (in_array ('blue', $lines))
  echo "in";
but still doesn't work

I read that in_array() is binary-safe, but dunno if it relates to my problem

#4 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 20 July 2006 - 09:32 AM

Have you tried using file() instead of file_get_contents()? Also, what operating system is the code running on, as some OS's recognise a newline as \r and others use \n. You can check your arrays contents with print_r($array); <- allyways helpful when debugging!

#5 Hologram_

Hologram_
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 20 July 2006 - 10:00 AM

Yes i have tried file() also
I am using print_r() too, it shows everything ok
Array ( [0] => blue [1] => orange [2] => yellow [3] => green )
php runs on Apache under Linux

really dunno what I am doing wrong.

#6 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 20 July 2006 - 10:10 AM

Sorry, I really don't know what to suggest at this point, there doesn't appear to be anything obviously wrong with your code... Hopefully someone else will read your thread and offer some suggestions.

#7 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 20 July 2006 - 10:29 AM

Try this:
$string = "blue";

$lines = file('file.txt');

//echo '<pre>' . print_r($lines, true) . '</pre>';

                       // remove any whitespace chars from the lines
if (in_array ($string, str_replace(array("\r", "\n"), '', $lines)))
  echo "in";


#8 Hologram_

Hologram_
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 20 July 2006 - 10:33 AM

wildteen88 great thanks it works :) awesome
SemiApocalyptic thanks very much for your time




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users