Well my loyal readers (and you must be loyal if you've made
it this far). Here we are at the final installment of the
'ABC's of PHP' where we are going to put some of what we've
learned over the past couple of months into practice.
We're going to go step by step through a small script to
read the latest headlines from Slashdot.org, for those of
you who may be a little young or have never heard of
Slashdot, it's basically a news aggregation site, but with a
difference. All the news available there is aimed squarely
at geeks and nerds the world over. There are articles on the
latest tech, and what's going on in the heady world of
corporate I.T or just downright bizarre things that people
All where going to do is read the XML feed file from http://slashdot.org/
slashdot.xml and then parse the XML data using regular
expressions. If you are going to use this feed then please
take a few minutes to read http://slashdot
.org/faq/code.shtml and learn the rules and regulations
of using the feed, Slashdot is very open about what you can
do with the data, but they do ask that you respect their
wishes to keep server loads to a minimum. Please note also,
that there are better ways to work with XML in PHP and there
are a number of built in functions detailed in the PHP
manual that make this process much easier.
As the feed is very simple however, I decided to simply just
use 'preg_xxx' calls to elaborate on the
material from part 9 on using reg-ex calls, if
you where reading anything more complex then you would
almost certainly want to use the proper XML functions.
On with the script
The first thing where going to want to do is to actually
load the XML, this can be achieved by using the PHP
'file' command. The file command will load any
file from any supported file or stream type that PHP can
handle, and will then store each line of the file into a
single array containing one entry for each line in the file.
NOTE: I've deliberately NOT included any error handling
here, so if for some reason the script is not able to
retrieve the feed, you will get an error displayed at this
We then set a couple of default values, for variables we'll be using soon:
$in_story = 0;
$storys = array();
The 'in_story' variable is used as a flag to let the main
reading loop know when it is inside a pair of
<story> </story> tags in the XML file, the
'storys array' on the other hand will hold an
array of smaller arrays each containing one story described
in the XML.
Next we loop over each line in the loaded text array using
'foreach', if you remember our discussion on
arrays, this takes each element in the array one at a time
and in sequence and presents it to the inside of the loop as
a single variable, which in this case will be a single
foreach($file_contents as $line)
Inside this loop is where we perform the necessary actions
to extract the information from each story in the XML and
load it into an in memory array.
The first thing we need to do in this loop is to decide when we are, and when we are not inside a story (I'm not going to retype the XML here, but if you load the URL mentioned above into your browser, you'll clearly see the structure), this is handled by the "if then" decisions that look like this:
All they simply do is, when a line with "" appears,
we know that we are now in a story block, so we set up a new
empty array to hold the details, and we set 'in_story' to
true, the second one resets 'in_story' to false, and adds
the single story array to the bigger full list of story's
The rest of the loop is a sequence of 'preg_matches' wrapped
up in an if-then statement that only executes if the
'in_story' variable is true.
.. More preg matches follow here for each tag ..
When a 'preg_match' matches on a line containing a tag, we
then save the contents of that tag into the 'current_story'
array, once we come back out of that story the now filled
array is added to the main 'storys' array to form a list of
stories found in the XML feed.
They're pretty much all the same, but the one that grabs the date deserves a little extra attention:
Rather than just assign the found text to a single variable,
we use two functions that are part of the series of
functions for handling text (These are detailed in the
strings part of the PHP manual). The 'explode' function
takes a sequence of characters and a string, and returns an
array of single strings, created by splitting the larger
string on the boundaries formed by the character sequence,
If $name = "Peter-#-Shaw", was used with
'$result = explode("-#-",$name)' then you would
end up with an array called 'result' that would
contain 'peter' in element  and
'shaw' in element .
The trim function just removes excess white space from the
ends of the input, so '$result = trim(" Peter Shaw
")' would make 'result' equal to just 'Peter Shaw',
we often use trim and explode together, especially in cases
such as above where the boundary is a single space, this
prevents empty slots in the array by removing any space from
the edges that does not constitute a separator between
In our script here, the time and the date of publication are
separated by a space, so we use 'trim' &
'explode' to separate them into two individual items
in the array.
Once we reach the end of this 'foreach' loop
that's the hard part of the script behind us, the rest is
just to display the results.
The next part down creates another array, this time from
This array holds the styles for the script to allow us to
use CSS inline to style the results. Now normally, you would
use a separate style sheet, and share it across the whole
site. Then use the HTML link tag, however in this case I was
reading the PHP manual chapter on the 'foreach'
function and I discovered something interesting
PHP 4 introduced a foreach construct, much like
Perl and some other languages. This gives an easy way to
iterate over arrays. Foreach works only on
arrays, and will issue an error when you try to use it on a
variable with a different data type or an uninitialized
variable. There are two syntaxes; the second is a minor but
useful extension of the first:
foreach (array_expression as $value)
foreach (array_expression as $key => $value)
Basically, from PHP5 onward, you can use the second version
with '$key => $value' so instead of your loop
variable getting the value of the variable, it gets the name
of the array key, or the bit that sits inside the [ &
] brackets when you create an array.
This got me thinking, and since I discovered that you can
also use spaces in array identifiers, I realized I could
store style rules in a normal array, then insert them into
the code using a loop like the following:
Which you can see round about line 111 in the script
attached to this article.
This opens up some interesting possibilities to generate
style sheets on the fly using database information and PHP
includes, which I'm going to explore at a later date, and
maybe even write an article on PHP builder about it.
The rest of the script are just a mass of print statements
that output the required HTML tags to create the page, and a
main loop that goes over the story's array printing each
story one at a time, all pretty easy to understand.
I hope you all enjoyed this series, I would have loved to
keep going with it and moved onto more advanced things, such
as databases, and classes and a whole host of other things.
Unfortunately time constraints and other work won't afford
me the time to do so.
I am a member on the PHPBuilder forums under the name
'shawty' so feel free to send me a PM or look out for my
posts in there. I do actively participate in the forum when
I have a few minutes here and there, I can also be found on
linked in at:http://www
.linkedin.com/in/petershaw08. I also have an MSN live
spaces blog at: http://cid-
4515677bdf99b35f.spaces.live.com/ Where I jot down
little snippets of technical goodness and other geeky type
stuff, feel free to drop me a visit and leave your mark in
my guest book.
Please remember, programming is not about just creating
code, or about doing as other have done before, it's a whole
art-form in itself. I made my first leap into this voyage of
discovery way back when I was just 7 or 8 years old, and
even now I still learn new things every day, the proof in
this is above where I discovered an extension to the
Always try to think outside the box, don't be tied in to the
constrains of what something can do, because that's what
it's supposed to do, I've used PHP for some pretty bizarre
stuff over the years and developed tricks that you'll never
find listed in any text books, and if you try something, and
it fails So what so you failed big deal, re-think what
you where attempting and learn from your mistakes, because
next time it'll help you make more creative screw ups,
believe me ;-).
The biggest thing though is "Have fun doing what you do"
because when it's fun, it makes the process all the more
enjoyable, and programming should always be fun.