Do we really need another browser?

Tuesday, 23 December 2008

View Comments

For those of you that spend a large amount of your day adjusting your styles (and occasionally javascript) to work on a variety of browsers you may be moaning over the thought of IE releasing another browser.

It's great that they are trying to improve things over at Microsoft, but I'd like to point out a few things as to why this could cause more pain than joy.

Looking at the stats over at w3schools which gives us a rough indicator of the break down between IE6 and IE7, they both have pretty close market share. Although IE7 is now in front it has taken some time to get there and IE6 is still refusing to die. With the introduction of IE8 we may be looking at three browsers in existence all with a reasonable amount of market share. Enough for those people who you are designing for to ask that there flashy new web site still renders well in all three browsers.

A couple of years back we were developing for IE6, IE7, Opera (and then Safari) and Firefox. We now have Chrome and IE8 now thrown into the mix. The biggest problem is that all these browsers render a page differently. Meaning more work with each new addition.

I don't want to go into detail about why it is good to have standards; it just seems with each release each of the majors does most things right, but it's the little things (except for IE6) that make life so hard.

I read this blog today on zdnet about which "big" sites had not yet prepared for IE8. I thought it was an interesting point of view that it was expected that these sites should be ready for another browser that has not yet been released. The other thing that the article mentions is the new 'Compatability View' button. This will allow (from what I have heard) sites to be rendered according to the way they would in IE6 or 7.

So... it has taken almost two years for half of the IE users to come on board with IE7. Why would they bother going to IE8 when it won't render pages the same unless you click on a button and then it will just be the same as the browser they just gave up.

It seems they still haven't got it right, with IE8 not passing the ACID3 test although according to Microsoft it apparently passes ACID2.

The past couple of years have been quite interesting with XP still refusing to die and IE6 refusing to die. When the guys at Microsoft come out with something that's nice and does great tricks (ala Office 2007) people flock to it. It would be better if they took the same approach with IE and come out with something that is the best in the field and not something that is playing catch-up with a backwards compat. button.

As they keep delaying, let's hope they're listening and come out with something that is worthwhile for the end-user. Otherwise we will continue to see Firefox eat away at their market share. Also we live in hope in one day IE6 will go away.

Large sized mysql dump

Tuesday, 9 December 2008

View Comments

As mentioned previously I have been working recently converting over some old classic asp pages into php. It has also been a company decision for them to continue to run mssql in conjunction with php. This hasn't caused too many dramas and isn't really what this post is about, but just to give you some background.

Recently my manager supplied me with a CD with a 350+ meg sql dump from mysql and asked me to verify the data. I thought the nicest way to do this would be to have phpmyadmin up and running so that they could quickly browse through the results once the data had been imported. Especially as they are used to using mssql's SQL Server Management Studio.

After trying to import several times through the command line to import the dump back into the new database I had no success. I had tried increasing the max_allowed_packet in the my.ini file, but with no success. I kept getting MySQL Server has gone away errors.

After getting in contact with the person who did the dump I asked a few questions about how they had gone about verifying the dump and if they had any success importing the data back in. It turns out that I was on the right track. In order to get this file to import they had increased the max_allowed_packet size to a wopping 10000000M. I tried this and the import worked straight away.

Here is the entry for the my.ini for those who want to know:

I suggest switching it back to something more sensible after the import. If you want more idea about what the packet size relates to and how to change settings have a look here:

Classic ASP.. what a headache

Friday, 5 December 2008

View Comments

The company I'm working for currently is going through the process of transferring all their old classic asp sites over to php and adding new functionality at the same time.

In order to meet deadlines some of the new functionality is being added in classic asp and being converted over at a later date which is not the best way to do things but keeps things moving (please note: I wouldn't recommend this to anyone as an exercise).

One of the biggest troubles I've had is getting values out of arrays. With php if you have a variable and want to know what the hell is inside it you can just call var_dump or print_r and if it's an array it's quite easy to see that it's an array.

In classic asp it's not that easy.

One of the pages had a series of checkboxes as follows:

<input type="checkbox" value="1" name="selectioncriteria[]"/>
<input type="checkbox" value="2" name="selectioncriteria[]"/>
<input type="checkbox" value="3" name="selectioncriteria[]"/>
<input type="checkbox" value="4" name="selectioncriteria[]"/>

Which is a quite neat way to do things as you get all your selected values inside a single POST variable.

For php we can do the following: (Please remember to sanitise your data first folks)

foreach ($_POST['selectioncriteria'] as $criteria) {

For asp it's almost exactly the same: (It just takes half an hour of Googling to find out)

for each criteria in request("selectioncriteria[]")

Hope that helps someone from half an hour of frustration!

The importance of regular expressions - pt 2

Tuesday, 1 July 2008

View Comments

Following on from my previous entry on regular expressions, I'd like to go over some of the useful functions that are built in to php for dealing with regular expression.

The two most common functions that I think you will come across are preg_match and preg_replace.

We'll start by looking at preg_match. preg_match goes through the given subject to see if it matches the given pattern, which is the simplest form of this function. The function then simply returns 1 if if finds a match or 0 otherwise.

The next important thing to look at is the $matches variable. With a basic call we simply get the match at the first index of the matches array:
preg_match("/ello/", "hello", $matches);


will give:
[0] => ello

As stated on "If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on. "

So what we will see in $matches[1] etc. is the text that matched parenthesized subpatterns. Let's look at a simple example:
$subject = "hello";

preg_match("/he(l)+o/", $subject, $matches);


[0] => hello
[1] => l
You can see clearly that in $matches[0] we have the full text match and in $matches[1] we have the l that matched in the parenthesis. Note that as we are using preg_match it will only show the first match and not subsequent matches although the second 'l' also matches. This is covered in the function preg_match_all.

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.

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:

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


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.

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 = "";


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:

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

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
We can then just grab the child node's name and value.

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


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.