Explode not working properly

unknown problems with php!

Page 1 of 1

2 Replies - 5361 Views - Last Post: 02 March 2007 - 08:29 AM Rate Topic: -----

#1 nanascalala  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 31
  • Joined: 13-January 05

Explode not working properly

Posted 01 March 2007 - 10:22 PM

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.php
<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.php
<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?!

Is This A Good Question/Topic? 0
  • +

Replies To: Explode not working properly

#2 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: Explode not working properly

Posted 01 March 2007 - 10:34 PM

You could use file() instead of the way you're doing it now. It opens a file and each line is an element of the array.

Now to your problem...which is two fold.

1. You're not escaping the \ on line 24. It should be explode("\\" not explode("\".

2. Once that problem is fixed you will have another error on the end of that line. You have $contents[$line]; and it should be $contents[$line]);
Was This Post Helpful? 0
  • +
  • -

#3 nanascalala  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 31
  • Joined: 13-January 05

Re: Explode not working properly

Posted 02 March 2007 - 08:29 AM

That's exactly it! Thank you. I feel stupid now. I should've caught that.

Thanks again, works like a charm
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1