Hologram_ Posted July 20, 2006 Share Posted July 20, 2006 hi all,this is my code[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";}[/code]my problem is that in_array() returns false also when string is in arrayhave no idea how to solve thisthanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/15114-in_array-problem/ Share on other sites More sharing options...
zq29 Posted July 20, 2006 Share Posted July 20, 2006 First off, a tip:[code]<?php$theFile = file_get_contents('file.txt');$lines = array();$lines = explode("\n", $theFile);//The above can be replaced with just:$lines = file("file.txt");?>[/code]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... Quote Link to comment https://forums.phpfreaks.com/topic/15114-in_array-problem/#findComment-60884 Share on other sites More sharing options...
Hologram_ Posted July 20, 2006 Author Share Posted July 20, 2006 Hi SemiApocalyptic,file contains lines like this:[code]blueorangeyellowgreen[/code]I can find the string (e.g. "blue") in $theFile, but not in array $linestried your code, thanks[code]$lines = file("file.txt");print_r ($lines);if (in_array ('blue', $lines)) echo "in";[/code]but still doesn't workI read that in_array() is binary-safe, but dunno if it relates to my problem Quote Link to comment https://forums.phpfreaks.com/topic/15114-in_array-problem/#findComment-60895 Share on other sites More sharing options...
zq29 Posted July 20, 2006 Share Posted July 20, 2006 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! Quote Link to comment https://forums.phpfreaks.com/topic/15114-in_array-problem/#findComment-60899 Share on other sites More sharing options...
Hologram_ Posted July 20, 2006 Author Share Posted July 20, 2006 Yes i have tried file() alsoI am using print_r() too, it shows everything ok[code]Array ( [0] => blue [1] => orange [2] => yellow [3] => green )[/code]php runs on Apache under Linuxreally dunno what I am doing wrong. Quote Link to comment https://forums.phpfreaks.com/topic/15114-in_array-problem/#findComment-60907 Share on other sites More sharing options...
zq29 Posted July 20, 2006 Share Posted July 20, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/15114-in_array-problem/#findComment-60912 Share on other sites More sharing options...
wildteen88 Posted July 20, 2006 Share Posted July 20, 2006 Try this:[code=php:0]$string = "blue";$lines = file('file.txt');//echo '<pre>' . print_r($lines, true) . '</pre>'; // remove any whitespace chars from the linesif (in_array ($string, str_replace(array("\r", "\n"), '', $lines))) echo "in";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15114-in_array-problem/#findComment-60918 Share on other sites More sharing options...
Hologram_ Posted July 20, 2006 Author Share Posted July 20, 2006 wildteen88 great thanks it works :) awesomeSemiApocalyptic thanks very much for your time Quote Link to comment https://forums.phpfreaks.com/topic/15114-in_array-problem/#findComment-60919 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.