Major glob() fail

I just had the pleasure of discovering another of PHP's little quirks and since it's been almost a year since my last post, I thought it would be a good occasion.

Working on some personal project that lists a bunch of stuff on my hard drive, I found out that directories that contain square brackets (those []) don't return any results for the simple reason that glob reads [stuff] as a character class, just like in regular expressions. When you know it it makes perfect sense, but when you don't, the documentation is really not so helpful. Of course it mentions libc's glob() and unix shells, but not everyone knows what that implies at first glance.

My first reaction when I noticed that those directories were missing was to try and escape them with backslashes, which works on unix systems, but not on windows since the backslash is the directory separator. The cross platform solution is to enclose them in brackets (i.e. [[]), which effectively creates a character class with only the opening bracket in it, so it matches correctly.

I then wrote this glob_quote function which, just like preg_quote, escapes the meta characters that glob uses.

function glob_quote($str) { 
    $from = array( '[', '*', '?'); 
    $to = array('[[]', '[*]', '[?]'); 
    return str_replace($from, $to, $str); 
}

Another detail worth noting while I'm at it is that this problem also occurs when you do glob('*.txt') if your cwd contains brackets, since in this case the cwd is pre-pended to the pattern, the solution being to escape it as well as such:
glob(glob_quote(getcwd()).DIRECTORY_SEPARATOR.'*.txt');

That's it for today, so until next year..

December 02, 2009 // PHP // Post a comment

Multiton base class

While I like the Singleton pattern every now and then, I prefer the flexibility that the Multiton potentially offers, and well it's just an extended version of the Singleton, so it's "compatible" with the Singleton model.

Anyway, to the point, PHP5.3 is coming, and with Late Static Binding you can do a base Multiton (or Singleton if you insist), which wasn't possible before. Now I like this very much because you can simply extend it rather than rewriting those (few, I know, but still) lines each time.

Continue reading...

December 23, 2008 // PHP // Post a comment

Dwoo v1.0 is out

Now that Dwoo's user base has grown a bit and that I've received enough feedback and fixed quite a bunch of bugs and design flaws, I feel confident it's time to go stable, so here comes Dwoo 1.0.0

For those that missed it, Dwoo is a template engine compatible with Smarty templates, with a lot of new features and syntax sugar and a new PHP5 codebase, if you want to read more I suggest you have a look at my earlier post, and its website.

Continue reading...

October 22, 2008 // PHP // Post a comment

Who let the Dwoo out ?

Four months have passed since I started on this project and I finally feel that it is stable enough to make an announcement and have more people trying it.

So what is it ? Dwoo is a PHP5 template engine. Another one you might think, indeed but with every new project comes a new vision, and hopefully you will like mine.

Continue reading...

May 12, 2008 // PHP // Post a comment

PHP RSS Generator

As part of my work on my CMS (and website as this site is running on it), I decided to code a RSS feed builder. This could easily be made as a standalone class so I thought I would share it.

The package is made of two files, one being the feed and the other one representing an item (i.e. an article). It works nicely as you can see from all the feeds I set up in here, and it supports the RSS2.0 standard entirely.

December 18, 2007 // News, PHP // Post a comment

ActionScript 3 Language file for GeSHi

As I wanted to post AS3 code with syntax coloring, I researched it a bit and found GeSHi, a PHP syntax highlighter, for which you can create language files quite easily. There is currently no AS3 file available in GeSHi though, so I decided to build one.

For now it's available here, but I hope it will make it into GeSHi's next releases. It's following the FlexBuilder2 colors for the default style, but it is stylable with CSS.

Should you see any missing keyword or anything, please contact me so I can update it.

Edit : I updated this file to v1.0.1, I was so focused on scraping data that I forgot the "this" keyword in the process, that's now fixed.

Download : AS3 Language file

November 30, 2007 // ActionScript, PHP // Post a comment

First page< Newer entries 1 2 3 4 [5]