Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 136,138 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,063 people online right now. Registration is fast and FREE... Join Now!




Explode not working properly

 
Reply to this topicStart new topic

Explode not working properly, unknown problems with php!

nanascalala
1 Mar, 2007 - 09:22 PM
Post #1

New D.I.C Head
*

Joined: 13 Jan, 2005
Posts: 31



Thanked: 1 times
My Contributions
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
CODE
<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
CODE
<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?!
User is offlineProfile CardPM
+Quote Post

snoj
RE: Explode Not Working Properly
1 Mar, 2007 - 09:34 PM
Post #2

$Null
Group Icon

Joined: 31 Mar, 2003
Posts: 3,304



Thanked: 6 times
Dream Kudos: 700
My Contributions
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]);
User is offlineProfile CardPM
+Quote Post

nanascalala
RE: Explode Not Working Properly
2 Mar, 2007 - 07:29 AM
Post #3

New D.I.C Head
*

Joined: 13 Jan, 2005
Posts: 31



Thanked: 1 times
My Contributions
That's exactly it! Thank you. I feel stupid now. I should've caught that.

Thanks again, works like a charm
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 10:40PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month