Ok, i'm setting up a site to generate a certain Walgreens store location.
I have a giant list of the walgreen address's in /txt/address.txt
I can display the whole line, but when i try to explode that line into the individual parts, i get an error. as hard as i try, i cant fix it. Help!
This is the working
test.phpCODE
<html>
<head>
<?
$dir = "txt/";
$files = files_in_dir($dir); // Array of all files in directory
$random_num = rand(0, count($files)-1);
$filename = $dir . $files[$random_num];
// Read only, getting file pointer
$fd = fopen ($filename, "r");
// Get whats in the file
$contents = fread ($fd, filesize ($filename));
// Close the file now that were done with it
fclose ($fd);
// Make an array by breaking up the contents of the folder by newline
$contents = explode("\n", $contents);
// $line contains one line picked randomly from the array
$line = rand(0, count($contents)-1);
// Takes a directory and returns an array of all files contained
function files_in_dir($fromHere, $xt = '', $xclude = '') {
$handle = opendir($fromHere);
$i=0;
// Read contents omitting . and ..
while( $file = readdir($handle) ) {
if ($file != "." && $file != "..") {
$dir_array[$i] = $file;
$i++;
}
}
// Exclude specific files
$i = 0;
if( is_array($xclude) && is_array( $dir_array ) ) {
foreach( $dir_array as $c ) {
foreach( $xclude as $x ) {
$c = strtolower($c);
$x = strtolower($x);
if( $c == $x ) {
unset( $new_dir_array[$i] );
break;
}
$new_dir_array[$i] = $c;
}
$i++;
}
$dir_array = $new_dir_array;
}
closedir($handle);
return $dir_array;
}
?>
</head>
<body>
<?php echo $contents[$line]; ?>
</body>
</html>
Aaaaand this, is the nonworking
test2.phpCODE
<html>
<head>
<?
$dir = "txt/";
$files = files_in_dir($dir); // Array of all files in directory
$random_num = rand(0, count($files)-1);
$filename = $dir . $files[$random_num];
// Read only, getting file pointer
$fd = fopen ($filename, "r");
// Get whats in the file
$contents = fread ($fd, filesize ($filename));
// Close the file now that were done with it
fclose ($fd);
// Make an array by breaking up the contents of the folder by newline
$contents = explode("\n", $contents);
// $line contains one line picked randomly from the array
$line = rand(0, count($contents)-1);
// SPLIT THE ADDRESS <------------DIFFERENCE
$contents = explode("\",$contents[$line];
// Takes a directory and returns an array of all files contained
function files_in_dir($fromHere, $xt = '', $xclude = '') {
$handle = opendir($fromHere);
$i=0;
// Read contents omitting . and ..
while( $file = readdir($handle) ) {
if ($file != "." && $file != "..") {
$dir_array[$i] = $file;
$i++;
}
}
// Exclude specific files
$i = 0;
if( is_array($xclude) && is_array( $dir_array ) ) {
foreach( $dir_array as $c ) {
foreach( $xclude as $x ) {
$c = strtolower($c);
$x = strtolower($x);
if( $c == $x ) {
unset( $new_dir_array[$i] );
break;
}
$new_dir_array[$i] = $c;
}
$i++;
}
$dir_array = $new_dir_array;
}
closedir($handle);
return $dir_array;
}
?>
</head>
<body>
<?php echo $contents[0]; ?><br>
</body>
</html>
You can see the difference at the top where you see // SPLIT THE ADDRESS.
Aside from that, the only difference is the last echo. I'm trying to display just the street number and street in test2.php, but as you can see its not working. WHAT AM I DOING WRONG?!