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:
array( // 'entity' => 'char' 'amp' => '&', 'lt' => '<', 'gt' => '>', 'apos' => '\'', 'quot' => '"' )
Example usage:
include('funcs.xmlentities.php'); $s = '<strong>This</strong> should be safe, but don\'t assume!<br/>'; print '<Field>'.xmlentities($s).'</Field>'; // outputs: <Field><strong>This</strong> should be safe, but don't assume!<br/></Field>
You can get the script here, or there’s a demo here too.
Breaking down large CSV files
Today I received a 45Mb CSV file for importing into a database… Needless to say the application we were importing to didn’t seem to like the size of the file, for what ever reason… So I knocked up a quite bash script to create smaller ‘chunks’ defined as a number of lines, to make importing simpler.
I’m sure there’s many way in which is can be simplified, so if you know any I’d like the contributions!
It’s run like this:
$ ./csv-chunk.sh large-data.csv 5000
The first argument being the filename and the second argument the maximum number of lines for each ‘chunk’. From that 45Mb megalith, 38 files of around 1.2Mb were produced which didn’t seem to break the other end!

Recently I’ve seen a few implementation of Growl in Javascript and basically just thought I’d have a go too. Everyone’s doing it, or so it seems!
This simple implementation is styled using pure CSS and you should be to easily modify it to suit your needs!
I’ve set up a demo page and the files can be grabbed from here.
Apache, Subversion and favicon.ico
I discovered a strange error in my apache logs today:
[Tue Oct 20 11:04:10 2009] [error] [client 12.34.56.78] (20014)Internal error: Can't open file '/.../svn/repositories/favicon.ico/format': No such file or directory [Tue Oct 20 11:55:55 2009] [error] [client 12.34.56.78] Could not fetch resource information. [500, #0] [Tue Oct 20 11:55:55 2009] [error] [client 12.34.56.78] Could not open the requested SVN filesystem [500, #2] [Tue Oct 20 11:55:55 2009] [error] [client 12.34.56.78] Could not open the requested SVN filesystem [500, #2]
Most of my Google searches returned issues where people couldn’t view their repositories, but I could see mine fine…
Then I looked at the error messages again and noticed it was looking for a repository called favicon.ico. A quick Google later and I found this page:
http://www.trilithium.com/johan/2005/02/no-favicon/
Adding the suggested items to my Apache conf fixed the problem!
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.
<?php require('class.csv.php'); header('Content-type: text/plain'); $csv = new CSV('test.csv'); while ($row = $csv->read()) { print_r($row); }
jQuery Ajaxify – Update
I’ve been doing more work on the jQuery AJAXify plugin, it’s now a lot more robust (I think…) and I’ve squashed a few bugs that were in the previous version (with help from Andrea Battaglia) along with adding in a few new features (with their own bugs no doubt!).
Theres an updated test page and as ever a direct link to download here.
Check out the test page to see the new features!
Edit: Further updates, all links have been updated to version 0.3b. Added (and documented existing) callbacks as requested by Sebioff! And a further fix for the ‘append’ option. Also fixed IE6 compatibility.
Further Update: Another update after the comment from David Lee about $(‘form’).serialize();
Another Further Update: I have set up a Google Code repository for this plugin, which states that this code is released under the MIT license.
Update October 2010: I have updated this plugin for jQuery 1.4.3 as a ‘lite’ version.
DNS Issues
Looks like this blog’s been down for a few days…
I’ve moved my DNS over to 123-reg and have had a few issues getting all the DNS records to work as expected…
Hopefully I’ve fixed it now…
CakePHP: Select Box Pagination
Using CakePHP’s built in pagination system has saved me so much time in my current day job, but the latest designs I’m working on have drop-down box style pagination, as it’s possible to get many pages of results.
To change the named parameters in the current URL I’ve created this small javascript function that will amend the URL: read more…
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( 'Session' ); 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() { $this->Session->delete('Counter.i'); } }