unknown101 Posted April 28, 2008 Share Posted April 28, 2008 Hi Guys, Just wondering if anyone could help me to read in values from a text file and then use this to populate an array. $myFile = "Filename"; $fh = fopen($myFile, "r"); $line = fgets($fh); So basically I want to read in each value (stored on seperate lines) and then put each one into an array.. could anyone give me an example of this or point me in the right direction? Thanks in advance Link to comment https://forums.phpfreaks.com/topic/103283-reading-values-from-a-txt-file-to-populate-an-array/ Share on other sites More sharing options...
moselkady Posted April 28, 2008 Share Posted April 28, 2008 Use file() function: $array = file($myFile); This reads each line into a string array Link to comment https://forums.phpfreaks.com/topic/103283-reading-values-from-a-txt-file-to-populate-an-array/#findComment-528972 Share on other sites More sharing options...
kenrbnsn Posted April 28, 2008 Share Posted April 28, 2008 You can do this at least three different ways: One: <?php $array = array_map('trim',file('Filename')); echo '<pre>' . print_r($array,true) . '</pre>'; ?> Two: <?php $array = explode("\n",file_get_contents('Filename')); echo '<pre>' . print_r($array,true) . '</pre>'; ?> Three: <?php $myFile = "Filename"; $array = array(); $fh = fopen($myFile, "r"); while (!feof($fh)) { $array[] = fgets($fh, 4096); } fclose($handle); echo '<pre>' . print_r($array,true) . '</pre>'; ?> Ken Link to comment https://forums.phpfreaks.com/topic/103283-reading-values-from-a-txt-file-to-populate-an-array/#findComment-528978 Share on other sites More sharing options...
unknown101 Posted April 28, 2008 Author Share Posted April 28, 2008 Thanks for the quick reply guys. Mayb you see the problem with this code... do { $names = array("$line"); foreach ($names as $value) { $count++; echo $count; echo "Search: $value<BR>"; if ( stripos($source, $value) ) echo 'The website grabbed has '. $value .' in it'; else { echo 'The site does not have the search keyword in it<BR>'; } } } while ( $line = fgets($fh, 1000)); Say for example $source variable contains all the text from a website. Ive put in 4 values in the text file which i know are in the site, but it only prints out that its grabbed the keyword on the last value not the first 3?? It goes through the loop the correct amount of times but just doesnt say its found the word. Watever word I put last in the txt file it works with sucessfully? Do you have any suggestions as to why this is happening? Thanks in advance Link to comment https://forums.phpfreaks.com/topic/103283-reading-values-from-a-txt-file-to-populate-an-array/#findComment-528988 Share on other sites More sharing options...
moselkady Posted April 29, 2008 Share Posted April 29, 2008 If I assume that any line in your file has more than one word, you can replace this line: $names = array("$line"); to: $names = split(" ", $line); // if words are separated by spaces // or $names = split(",", $line); // if comma-separated The other thing is that you should have a $line = fgets($fh, 1000) statement before your do loop, or replace your do .. while loop into simply a while loop. Link to comment https://forums.phpfreaks.com/topic/103283-reading-values-from-a-txt-file-to-populate-an-array/#findComment-529582 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.