Tags: php — March 24, 2005 (0 comments)
This more or less does exactly what it says on the tin: parse CSV into a PHP array. It has serveral advantages over fgetcsv, such as, even if auto_detect_line_endings is set as false in php.ini, it can still cope with all Windows, Mac and *nix line breaks. As basslines at gmail dot com says, it also can't cope with the PHP escape character in the CSV, while this can. That's all I can think of off the top of my head...
To call the function, you can use something like easy_csv (file_get_contents('2005.csv')); if your CSV uses the default "," delimiter and is at 2005.csv, to specify a non-default delimiter, you would use something like easy_csv (file_get_contents('2005.csv'), ';'); if you're CSV uses the ";" delimiter.
You can, of course, put the CSV directly in the function, like easy_csv ('Australia, Malaysia, Bahrain, San Marino, Spain, Monaco');
As for the code itself, here you go:
<?php // Define the function easy_csvfunction easy_csv ($csv, $delimiter = ',') { // If it's got a \n line break (Windows style) then seperate lines into an enumerated arrayif (strpos($csv, "\r\n")) {$csv_lines = explode("\r\n", $csv); // If it's got a \r line break (Mac style) then seperate lines into an enumerated array} elseif (strpos($csv, "\r")) {$csv_lines = explode("\r", $csv); // If it's got a \n line break (*nix style) then seperate lines into an enumerated array} elseif (strpos($csv, "\n")) {$csv_lines = explode("\n", $csv); // If it's not got any of the above line breaks, then just dump the whole thing into $cvs_lines} else {$csv_lines = $csv;} // If $cvs_lines is an array (aka. has more than one line)if (is_array($csv_lines)) { // Set $i to 0$i = 0; // Loop through the linesforeach ($csv_lines as $csv_line) { // Put each piece of data as part of the array$csv_lines[$i] = explode($delimiter, $csv_line); // Add 1 to $i$i++; // End foreach ($csv_lines as $csv_line)} // If it isn't an array (aka. has one line)} else { // Put each piece of data as part of the array$csv_lines = explode($delimiter, $csv_lines);} // Return the array of datareturn $csv_lines; // End the function easy_csv} ?>It produces an array like:
Array
(
[0] => Array
(
[0] => Australia
[1] => Malaysia
[2] => Bahrain
[3] => San Marino
[4] => Spain
[5] => Monaco
[6] => Europe
[7] => Canada
[8] => USA
[9] => France
)
[1] => Array
(
[0] => Britain
[1] => Germany
[2] => Hungary
[3] => Turkey
[4] => Italy
[5] => Belgium
[6] => Brazil
[7] => Japan
[8] => China
[9] => Total
)
)
Tags: aside, fwd — March 23, 2005 (0 comments)
There was a post at Engadget yesterday about the truth of the Super Shuffle, read it here.
Tags: holiday, hosting — March 22, 2005 (0 comments)
After the downtime in the last week, I'm closer to moving hosts than ever, while nothing is certin, it seems as if I'll be moving to ithium sometime in my Easter holidays (26th March till 19th April), I can immediately discount the first week, as I'm away on holiday in Majorca. The downtime was caused by my host having problems changing host.
I brought all of my websites back online over a period of a couple of days, doing each MySQL table at a time, and adding the subdomain when the site was ready to go back online, however, now everything is back online, it's faster than ever.
When I do move to ithium, they'll be almost no downtime, because I'll get the account, upload all my files, get all the MySQL databases ready, so as soon as the DNS changes, we'll be on a different server. It should also reduce the amount of downtime normally, which is good for both me and you :). It also allows me to properly start one of the projects I never really started, Speed PHP, as my current host doesn't have enough permissions on the server (only having a reseller acount) to do what is needed, however, I'll give you more on Speed PHP at a later date.
Also, almost all development of everything has come to a stop, as I've got other, more important things to do, like relax, after spending a couple of months of web developing. I personally recommend Gran Tourismo 4, brilliant game ;). On a more serious note, I do have real things to do, which need to be done.
Apart from that, there's little to say, apart from the fact I've quitely put version 2 of my wishlist online :). Remember, it's my birthday on the 20th April (yep, first day of term, aren't I lucky ;)). At least something to smile about has happened :P
Tags: php — March 11, 2005 (0 comments)
This is the first real project I've started working on, although it is still primarily for myself, it doesn't mean I'll be compromising things for other people using it just for me. Although this is only version 0.8.1, it already has some features that I wouldn't of put in if I was doing it just for myself.
Anyhow, time for more detail, like what the hell it is. MyDB is a lightweight, yet powerful, database class. It grew out of part of Location Unknown, I was looking at various database engines, but there was always something wrong, I ended up deciding that I'd write my own. It took me around a day to plan and code the original MySQL version. After a couple of days of thinking about it, I decided to untie it from MySQL, adding support for other databases, such as PostgreSQL and SQLite. This lead to the class getting two extra functions, to keep one of the principals I had set myself when I decided to untie it from MySQL - Make sure as little as possible has to be changed when changing what database server you're using.
That pretty much tells you almost everything you need to know about it in one paragraph. But that doesn't mean I've got nothing more to say.
One of the principles I set out right in the very beginning was it would have very clear, commented code, as to make it easy for other people to build upon it, and hopefully release it to the web developer community. Modify it to your need, and if you're kind, release your code (in accordance with the license, of course).
Personally, I doubt I'll ever need anything more than MySQL, but I'm going to try and support PostgreSQL (anyone who can give me PostgreSQL hosting will be thanked) and SQLite, one of the main principles I set out when I decided to make it support more than one database server, was I'd make it so you have to change as little as possible, and that's likely to just be the connect function...
What licence am I going to release it under? I want this to be open source, yet I still want control over it, I decided to go with the Creative Commons Attribution-NonCommercial-ShareAlike 2.0 License. I'll explain my reasons for each part:
- Attribution
- I don't want this script to be slightly modified, and then be given no credit at all
- Noncommercial
- Many complex reasons, although, I'd like to say, anybody who wants to use it commercially, please do not hesitate to contact me
- Share Alike
- To stop people from changing the license to one that I count as inappropriate
Please, check it out, download it, or at very least visit it's website at http://mydb.geoffers.uni.cc/.
Tags: wordpress — March 10, 2005 (0 comments)
With WP 1.5 going gold, a lot of plugins are now becoming available for it, some of these being anti-spam plugins, I've installed a couple, so welcome to a spam free zone.
One of the two plugins I installed was Eric Meyer's brilliant WP Gatekeeper 1.5 RC1 - on the comment form, it displays a stupid question, which, though blindingly obvious to humans, stops bots from posting :)
The other, was WPBlacklist, which runs off the MT Blacklist, maintained by Jay Allen.
These measures, I hope will help to make this a spam free zone, without me having to delete them personally :)