The importance of regular expressions - pt 1

Monday, 30 June 2008

View Comments

Sometimes when asked by a junior developer the cleanest way to solve a problem, if the answer is 'regular expressions' this is usually followed by a groan or a sigh or a combination of both.

The truth is if you can learn how to handle regular expressions early on you will have one of the most simplest, cleanest and efficient tools in your arsenal to take on a variety of problems.

The reason regular expressions are so useful is that they can be used to target what you need in any string with the flexibility of being able to use your matches to process the given string.

php has a variety of powereful functions that can be used in conjunction with regular expressions, but before I go in to that I want to point out some of the best places to find out about regular expression so you can start learning and practicing.

To get your head around the basics it's best to try and track down somewhere that explains regular expressions in detail, I don't want to go in to that here as there are 100's of pages about this already. The place I recommend looking at is here, which explains clearly from a php perspective how regular expressions work. This is a great site for new beginners and for anyone who wants to check-up rules when you're getting stuck.

So once you've had a play around with a few basic expressions and you're still banging your head on the table and everyone is looking the other way when you cry out "who can help me with my regex!", don't panic, there is help! What you're going to want is one of these a regular expression tester. This has saved me many a time when something isn't matching or it's matching too much or too little. You can simply plug in your expression and it will show you exactly where it's matching and you can tweek your expression quickly without having to commit your changes to a repository etc.

Hopefully this is enough to get you started. As everyone knows practice makes perfect, which is the same with regular expressions. Hopefully with your cheat sheet and regular expression checker it will help you avoid most simple headaches. In my next post I will look at some useful php functions to help deal with processing regular expressions.
more...

Getting certain files from a directory

Friday, 27 June 2008

View Comments

Recently while reading the book Professional LAMP: Linux, Apache, MySQL, and PHP5 Web Development I came across a great piece code that can be used to retrieve an array of filenames from a directory matching a certain pattern. Unfortunately the function in the book doesn't quite work with the current definition of that function, but with a bit of tweeking we can get it to do the same thing.

Previously we would use preg_grep along with scandir to match the files in a directory to a certain pattern. As follows:

$files = preg_grep('/\.php$/', scandir('./'));

Using the glob() function we can now easily search for a variety of file types. With the glob() function we have a bunch of flags, the one we are interested in is GLOB_BRACE which will allow us to search for more than one type of file extension.

Our function call will incorporate the directory we want to search in, the file types we want to search for and our GLOB_BRACE flag.

Our directory: "./"
Our file types: "{*.jpg,*.gif,*.png}"
Our flag: "GLOB_BRACE"

Put it all together and we get the following call:

$filelist = glob('./{*.jpg,*.gif,*.png}', GLOB_BRACE);


And the result when we print it out:


Array
(
[0] => ./library.jpg
[1] => ./hi.jpg
[2] => ./test.png
)



Sweet!
more...

Processing XML with php5 and DOM extension

Tuesday, 17 June 2008

View Comments

Depending on what you are using php for, parsing XML documents may not be something you do regularly, but it's something that's quite useful to know.

The following is a great article which is a good starting point for processing XML documents using both simple XML and the DOM extension.

http://devzone.zend.com/node/view/id/1713


The basic idea is as follows:
  • Load the document
  • Grab whatever elements your processing i.e Books or Shares etc.
  • Loop through the elements and extract whatever info you want from them

For loading in our document you can provide either a local file or a URL to an XML file.


$dom = new DOMDocument('1.0', 'UTF-8');

$doc = "http://www.example.com/test.xml";

$dom->load($doc);


Once our document had been loaded into the $doc variable you can then grab the elements you are after.

$books = $dom->getElementsByTagName("book");

In this case we are grabbing all the book elements in the document. Our XML document may look something like this:


<library>
<book>
<name>Red Riding Hood</name>
</book>
<book>
<name>3 Little Pigs</name>
</book>
</library>


Its important to note that $books is not an array of elements but a special grouping of nodes called a DomNodeList. If you want to get elements from the list you cannot just access them like this:


echo $books[0]['title'];


Although we can still access individual elements by calling $books->item(0) but again this returns a particular node element and not an array.

Our next step is looping through our book elements and getting the required information we want.



$data = array();

foreach ($books as $book) {
foreach ($book->childNodes as $info) {
$info_array = array();
if ($info->nodeType == 1) {
$info_array[$info->nodeName]['name'] = $info->nodeName;
$info_array[$info->nodeName]['value'] = $info->nodeValue;
$data[] = $info_array;
}
}
}

We first loop through each of our books, then through each books child nodes. If a child node is type 1 which is an element node we want to extract the information from that node. The full list of node types can be found here http://au2.php.net/manual/en/domxml.constants.php.
We can then just grab the child node's name and value.

Hope that gives you a basic idea of how it's done.

TR
more...

First post

Hi to anyone who stumbles across this blog. I'm not sure if this will ever see the light of day.
The main idea for me to set up this blog is for me to store somewhere all the bits and pieces of info related to php that I've picked up so that I can remember where I found it and how to do it.
Over the past couple of years I've found many helpful sites and thought I could remember the stuff I used once or twice, but found this is never the case.
So this blog is not really a tutorial to help others, it's more about helping myself. But if you manage to find something useful here then that's a bonus.

TR
more...