Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-20-2006, 11:58 PM   #1
Registered: Apr 2005
Distribution: Ubuntu, RHEL, Darwin
Posts: 73

Rep: Reputation: 15
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.
Old 04-21-2006, 06:00 AM   #2
Wim Sturkenboom
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
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'

Last edited by Wim Sturkenboom; 04-21-2006 at 06:18 AM. Reason: added directory listing
Old 04-21-2006, 08:37 AM   #3
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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()
Old 04-21-2006, 09:07 AM   #4
Registered: Mar 2003
Location: Berkshire, England.
Distribution: SuSE 10.0
Posts: 299

Rep: Reputation: 32
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.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 09:22 AM
Mozilla Linux Command Line URL Parsing Security Flaw Reported win32sux Linux - Security 9 10-06-2005 07:39 AM
Substitute String or line in a file dimsh Linux - Newbie 4 09-21-2005 04:26 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 11:24 PM
Converting a paragraph into a one-line string ganninu Linux - General 4 12-03-2003 08:50 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:38 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration