simpleGallery 0.2 – Minor updates

I’ve updated the gallery script I created in July. I’ve re-factored the code into a static class for easier modification and have created a config.ini settings file, to save modifying the script itself.

New features:

* Image titles
* Separate configuration
* Standard mode with safe URLs as well as mod_rewrite enabled ‘nice’ URLs
* Added ‘..’ parent folder to galleries

The demo page has been updated and the script is available to download here.

XML Entities in PHP

Because htmlentities() doesn’t even come close.

This small file contains 4 functions (2 of which are taken from the PHP manual, credit given!) which will allow you to encode and decode entities from ASCII/unicode strings in either decimal or hexadecimal format for use in valid XML documents.

The xml_entity_decode() function accepts an optional second parameter to allow non-standard XML entities (that may have been specified in your schema) in the format:

  // 'entity' => 'char'
  'amp' => '&',
  'lt' => '<',
  'gt' => '>',
  'apos' => '\'',
  'quot' => '"'

Example usage:


$s = 'This should be safe, but don\'t assume!
'; print ''.xmlentities($s).''; // outputs: <strong>This</strong> should be safe, but don't assume!<br/>

You can get the script here, or there’s a demo here too.

PHP CSV Reader

I’ve never really built a definitive CSV reader and end up building a quick implementation of one any time I need one.

But after working on the chunk script I decided that next time I build a CSV class I’d do it properly.

This is the result:


  header('Content-type: text/plain');

  $csv = new CSV('test.csv');

  while ($row = $csv->read()) {

There’s an information page here, and the script is here.

CakePHP: Components, redirect fail (On my part…)

I’ve been working on a CakePHP project lately and created a small component which was only needed in one of my controllers:

class CounterComponent extends Component {
  var $components = array(

  function i() {
    if ($this->Session->check('Counter.i')) {
      $i = ($this->Session->read('Counter.i') + 1);

    } else {
      $i = 0;

    $this->Session->write('Counter.i', $i);

    return $i;

  function clear() {

Continue reading “CakePHP: Components, redirect fail (On my part…)”

PHP: Object Oriented Image Manipulation

I’ve been working on a CMS lately and having to create thumbnails for uploaded images is always a pain, lots of maths working out the correct sizes and such, so I’ve created a fairly small script to manipulate images in an object-oriented style.

For example:

scale(400, 300);

$image2 = new Image('image2.jpg');
$image2->watermark = 'sample.png';

I’ve only implemented a few of the GD library methods, but I think these are the most useful methods. I might have to work on something that rounds the corners too, but I don’t have any need for it just yet.

There’s a demo page as ever, and you can get the script here (It’s a bit big, because the sample image is included, here’s one without it).

String Variable Concatenation

I stumbled across something odd today in PHP:

$r = '';
$r .= $r .= $r .= 'a';

Now, personally, I’d have expected a syntax error from the above code, but the result was even more confusing at first…

print $r; // 'aaaa'

Not sure if this was the expected output or not I tested similar code in other languages:


r = ''
r += r += r += 'a'
puts r # 'a'


r = ''
r += r += r += 'a'
#   File "", line 1
#     r += r += r += 'a'
#             ^
# SyntaxError: invalid syntax


var r = '';
r += r += r += 'a';
alert(r); // 'a'


my $r = '';
$r .= $r .= $r .= 'a';
print $r; // 'aaaa'

That explains it!

So the reason the string is ‘aaaa’ seems to be that the code is evaluated from right to left:

$r = '';
$r += $r += $r += 'a';

// How it works:
// $r += 'a'; // 'a'
// $r += $r += 'a'; // 'a' + 'a';
// $r += $r += $r += 'a'; // 'aa' + ('a' + 'a')

I don’t think it’s a bug, well, at least I assume not, but is there a name for this?

Update: I asked some clever people for help understanding it.