-   Programming (
-   -   Parsing a string line-by-line in PHP (

enigma_0Z 04-20-2006 11:58 PM

Parsing a string line-by-line in PHP
OK, here's my question: I'm redesigning my webpage in PHP, but I've got a problem. The way I have my pages set up is I have a directory filled with pages for that "section". The list of stuff for that section is filled out with a directory listing (and maybe this would work better with a mysql setup, but then I'd have to get into authentication and stuff too), but anyway.

My question is how can I parse a string line by line. What I'd like to do (and mind you this is largely psuedo-code) is this:


dirs = exec_shell('ls '.escapeshellarg($_GET['section']))
while (not end of string) do
    trim 2 chars off end of dirs[currentline];
    echo "<a src=view-text.php?section=foo&page=dirs[currentline]>bar";

Now I'm not saying that the string(s) has to be an array of lines, that's just one option. If I can substring for the contents between newlines "\n", that would work too. I just need options is all.

Once again, if someone could point me to a good tut on creating forms and stuff so I could use my mysql server and a content-mangment system of my own creation, that would be even greater.... yeah.

but anyway, thanks in advance.

Wim Sturkenboom 04-21-2006 06:00 AM

For tutorials on the web, do a search on 'php mysql tutorial'.

I tought myself using a series on; could not find it anymore on their site (previous time I checked it was no longer complete), but this is the same article (also incomplete). Although incomplete, it might get you started.

PHP and MySQL Web Development by Luke Wlling and Laura Thomson; isbn 0-67232-525-X

Sams Teach Yourself PHP, MySQL and Apache by Julie C. Meloni; isbn 0-67232-620-5

Personally I think the first book is very good. The second one is less comprehensive but might get you going a little quicker.
I still use both.

For an example of directory listing, see I've used it once with success (no use of thumbnails).
Or search the web for 'directory listing php'

graemef 04-21-2006 08:37 AM

The php manual suggest the following snippet of code:

$dir    = '/tmp';
$files1 = scandir($dir);
$files2 = scandir($dir, 1);


This is for the function scandir under the chapter on Directory Functions. This will return the . and .. directories but these can be quite easily ignored. In the same chapter the readdir function might help.

You can then build a loop looking for the files, using is_file() and directories using is_dir()

Spudley 04-21-2006 09:07 AM

If you're reading the data from a file, you can read it a line at a time pretty easily. See the file handling funcs in the php manual.

If you've got the whole string in memory you can split it into lines by doing an explode("\n",$string), and then loop through the resulting array.

All times are GMT -5. The time now is 09:01 AM.