(Solved) Please help: populate array() from text file?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
DeprecatedDiva
Forum Newbie
Posts: 24
Joined: Wed Aug 03, 2005 10:47 am
Location: NW Louisiana

(Solved) Please help: populate array() from text file?

Post by DeprecatedDiva »

Hi everyone. This is my first post. I am two weeks old in my exposure to & learning PHP and I am absolutely excited at the possibilities!

However, I've run into a problem. Well, not really a problem. Just a question of efficiency.

Background history: I have a customer who wants to minimize - AS MUCH AS POSSIBLE! - the risk of inadvertently screwing up the code that builds her pages. So, in light of that, I wanted to be able to populate the array from an external text file instead of having to prepopulate the field thus:

Code: Select all

$filename = array('index','contact','thisfile','thatfile',);
She would be the one building the text file where the input would simply be something along the lines of a shopping list:

Code: Select all

index
contact
thisfile
thatfile
and have that file populate the array.

Rather than bug folks, I've searched, googled, and re-searched until I am ready to throw in the towel. I am probably not using the correct search parameters or understanding the material I've read.

Is there somewhere a prepackaged code that I can follow until I gain more experience coding PHP myself?

Thank you in advance.
Last edited by DeprecatedDiva on Sun Oct 23, 2005 5:44 pm, edited 2 times in total.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I think you want to have a look at http://www.php.net/file.


In our starterpack threads you will find loads of hints where you can find the information you need...
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Hint: split();

Extra huge hint: \n marks a new line, but so does \r\n on windows so you'll need split() to check for \n \r\n and \r for newlines (\r on mac me thinks)
DeprecatedDiva
Forum Newbie
Posts: 24
Joined: Wed Aug 03, 2005 10:47 am
Location: NW Louisiana

Post by DeprecatedDiva »

:D Thanks for the tips guys! Now I gotta go read and learn....

Of course after posting this, I thought of another search and found the same information you folks have pointed me toward :oops: :D

I'm sure I will be back with more questions. Ciao for now!!!
DeprecatedDiva
Forum Newbie
Posts: 24
Joined: Wed Aug 03, 2005 10:47 am
Location: NW Louisiana

Post by DeprecatedDiva »

Okay guys, I've played with file() and it does as advertised. I've also played with split() and it does as advertised as well. But for the life of me, I just can't figure out how to get this:

Code: Select all

$textfile = 'includes/pagelist.txt';
$pagename = file($textfile) or die('Could not read file!');
where the contents of pagelist.txt is:
  • home
    contact
    thisfile
    thatfile
converted into this:

Code: Select all

$pagename = array('home','contact','thisfile','thatfile');
so I can use this:(code from http://digital-web.com/articles/easypeasy_php_2/)

Code: Select all

// 1. Define an array of allowed $_GET values:
    $pagename = array('home','contact','thisfile','thatfile');
// 2. If there is no $_GET['id'] defined, then serve the homepage:
    if (!isset($_GET['id'])) {
     include ($_SERVER['DOCUMENT_ROOT'] . '/includes/home.inc');
    }
// 3. If the page is allowed, include it:
    elseif (in_array($_GET['id'], $pagename)) {
    include ($_SERVER['DOCUMENT_ROOT'] . '/includes/' . $_GET['id'] . '.inc');
    }
// 4. If the page is not allowed, send them to an error page:
    else {
    header("HTTP/1.0 404 Not Found");
    include ($_SERVER['DOCUMENT_ROOT'] . '/includes/error.inc');
}
I just can't seem to wrap my mind around the necessary steps. :cry:

If you have any further examples or lessons that may help me, I would greatly appreciate a pointer in the right direction.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$lines = file('filename.txt');
$pagename = array_map('trim',$lines);
var_export($pagename);
DeprecatedDiva
Forum Newbie
Posts: 24
Joined: Wed Aug 03, 2005 10:47 am
Location: NW Louisiana

Post by DeprecatedDiva »

feyd, BLESS YOU! I looked at array_map earlier but didn't understand it would serve my needs. (I need a dunce cap....) It works like a champ! Now pages can be added as needed without having to modify the master files.

I think it's time to start from page one of the "book" and start playing with the examples....
Post Reply