[Scottish] Using PHP to parse simple 'text database'

Colin McKinnon scottish at mailman.lug.org.uk
Sun May 11 22:12:00 2003

David Marsh wrote:

>Any PHP wizards out there?
>(And is it OK to ask PHP questions here?)
>I'm thinking of using PHP to populate webpages from some homebrewed 
>'text database' files (rather than go to the hassle of MySQL), so as
>to save me having to type in lines and lines of HTML.. ;-)
(? you don't talk to the db with HTML - thats what the SQL & PHP is for)

>The next snag is how to then transmogrify this into a webpage.
>I'm a bit of a novice at PHP, so I don't know the full syntax of the
>language (and don't have time to read up on it all), but I reckon
>something like the following (pseudocode) must be possible:
<snip lots of code>

Yes, something like that would work - but you could use a proper DB 
 (even a fully functional DBMS) to do the same job with less code / 
complexity. If you're not all that interested in the details of the 
code, you can create functional applications based on PHP and MySQL just 
using a point and click interface.

Human readable ascii databases can be easy to work with - Unix has a 
long tradition of doing so, and a horde of tools (no pun intended) for 
working with them (Perl, Awk, sed, grep to name but a few) - why try to 
recreate them from scratch in PHP? Having said that, PHP does have some 
builtin functions that would ease your approach (check out the array 
functions & string handling functions - split() in particular). 
Alternatively if it is a modestly-sized completely self-contained data 
set, why not just store it in an array and serialize (see serialize() 
function) to dump it in a file.

Regarding your code - it's hard to comment further without some 
indication of what you might want to do with your data (or how much of 
it there is, how it is structured, how often it changes...) other than 
put it on a web page.

>Is anybody out there a PHP expert who knows how I could write that as PHP?
It's not far away from being executable PHP code - get a hold of the PHP 
manual (or read it online - http://www.php.net/manual/en/). You're 
almost coding it as if you were used to programming in 'C' - and that's 
a reasonable starting point - think of everything as a function with C 
type control structures and syntax (but not as strictly typed, and 
variables begin with a $) and you should have no problem in finding the 
stuff in the manual / writing the code.