PHP: Write and append to files with file_put_contents

The PHP file_put_contents() function is a useful shortcut for dumping a string into a file (compared with using fopen, fputs etc). By default it will create a new file if it does not exist, or overwrite the file if it does exist. Recently I re-read the documentation for this function and discovered it is also possible to append to a file with file_put_contents.

Categories PHP

Keyboard shortcuts for Excel for Mac

I have Microsoft Excel for Mac 2011 and have found somewhat annoyingly at least a couple of the shortcut keys aren’t the same on Mac as they are for the Windows edition. This post covers a couple of them and I will add to it as I find alternatives for common functions. Feel free to add a comment below the post if you have a shortcut key you know about that is different between the Windows and Mac editions.

Edit in cell: F2 on Windows = Control+U on Mac

F2 is a nice and easy shortcut key to edit a formula in the cell without having to double click the mouse. For whatever reason, Microsoft have decided to make F2 be “cut” in the Mac edition of Excel instead and there doesn’t appear to be any way to change the shortcut key in any of the settings.

Instead, use Control+U on a Mac to edit in cell. This is an awkward keyboard combination and not at all easy to do with one hand; I usually have to use two hands.

It is also possible to use a 3rd party app to remap F2 to Control+U for Excel. I haven’t used any of these (they’re all pay software) but apparantly it can be done with these: iKey, Keyboard Maestro, QuicKeys, and Butler

New line in cell: Alt+Enter on Windows = Control+Option+Return on Mac

If you want to create a new line in a cell use another awkward keyboard combo on a Mac: Control+Option+Return.

It’s not as awkward as Control+U but it would be nicer if it was simply Option/Alt+Return so it was the same as Windows. Incidentally, Option+Return is just the same as pressing Return so there’s no reason I can see that it shouldn’t be the shortcut for new line in cell.

Other shortcut key combinations that are different

Add any useful keyboard combos in the comments below that are different between the Windows and Mac versions of Excel.

Searching Gmail by date

This is a quick post for my own reference to show how to search Gmail by date range. I often write little tips like this for myself so I have somewhere easy to reference them as I forget this sort of stuff all the time 🙂

Server emails and purging the old ones

I get a lot of emails from servers addressed to a specific email address sending reports and all sorts of other stuff. I have filters in Gmail to put them into a specific folder automatically, skipping the inbox and I never actually see them. Emails reporting errors don’t get filtered in the same way so those always hit my inbox.

So these error emails are pretty much just there in case I need to look at them for some reason. Every now and then I like to purge out older server emails and the easiest way is to search for them by date range and then delete.

Searching by label and date range

These server emails get filtered into a label called “Servers”. To search for them prior to e.g. May 1st 2011 (so all emails from April 30th and earlier) do a search like this: 

label:servers before:2011/05/01

If you wanted to search for any email between say May 23rd and May 29th 2011, do this:

after:2011/05/23 before:2011/05/30
 

Note that the “after” date needs to be the date searching from (it includes that date) but “before” needs to be the day after you want to search to.

border-radius and box-shadow support across browsers

This post is a reference sheet I needed for myself to show which browsers support the CSS3 box-shadow and border-radius properties, which require vendor prefixes, and which have no support at all.

box-shadow / border-radius support

The border-radius support and box-shadow support columns indicate what property name is required for the property to work in that browser. For older versions of Firefox the are prefixed by -moz, for older versions of webkit they are prefixed with -webkit.

Browser and version border-radius support box-shadow support
Chrome border-radius box-shadow
Firefox 4.0+ border-radius box-shadow
Firefox 3.6 -moz-border-radius -moz-box-shadow
Firefox 3.5 -moz-border-radius -moz-box-shadow
Firefox 3.0 -moz-border-radius No support
Internet Explorer 9+ border-radius box-shadow
Internet Explorer 8 and earlier No support No support
Mobile Safari 4.0.5 (iOS 4) border-radius -webkit-box-shadow
Mobile Safari 4.0.4 (iOS 3) -webkit-border-radius -webkit-box-shadow
Opera 10.50+ border-radius box-shadow
Opera 10.10 and earlier No support No support
Safari 5.0 border-radius -webkit-box-shadow
Safari 4.0 -webkit-border-radius -webkit-box-shadow

Mobile Safari was tested on iOS3 and iOS4 using the iPad and iPhone simulators on a Mac, and also on Android 2.2 (which reports it as 4.0 and 533.1). The version on Android 2.2 was like 4.0.5 where it needed the vendor prefix for box shadow but not border radius.

Cross Browser Usage

In order to ensure the greatest cross browser usage, set up border-radius and box-shadow like so:

-moz-border-radius: [radius settings here];
-webkit-border-radius: [radius settings here];
border-radius: [radius settings here];

-moz-box-shadow: [shadow settings here];
-webkit-box-shadow: [shadow settings here];
box-shadow: [shadow settings here];

Replace [radius settings here] with the exact same settings for each; and the same for [shadow settings here].

Note: I used the HTML5 DOCTYPE when testing to create the above table.

Change the default sort order for files and images in SilverStripe

SilverStripe’s File, Folder and Image objects have a default sort of ‘Name’, ‘Sort’ and ‘Name’ respectively, but if you are also using Uncle Cheese’s DataObjectManager the sort order will change to the ‘SortOrder’ field. Unless you’ve been sorting these yourself they’re going to appear in the ImageField control in a more or less random order, which isn’t idea. This post shows the solution.

SilverStripe version used

I have used the methods succesfully in this post with SilverStripe 2.4.5 

Changing the default_sort for ImageField and FileField

In your mysite/_config.php file, add the following:

File::$default_sort = 'Name';
 Folder::$default_sort = 'Name';
 Image::$default_sort = 'Name';

Alternatively make Folder::$default_sort = ‘Sort’ to restore the default behaviour, but I personally prefer to keep it in name order.

Files and Images table

Changing the default_sort as shown above won’t affect the order for the table of files/images in the Files and Images section of the CMS. However the DataObjectManager allows you to change the sort order by clicking the column headings, so it’s not really an issue so much.

However, if you want to make it use the default sort instead, the only way I could work out how to do this myself is to remove the File class from the sortable classes.

In theory, you should be able to do this:

SortableDataObject::remove_sortable_class('File');

But it didn’t work when I tried it because it only removes the extension from the class and doesn’t remove it from the SortableDataObject::$sortable_classes array. You could do this instead, although it’s a but hacky and may have unexpected consequences.

Again, add this to your mysite/_config.php file:

SortableDataObject::$sortable_classes = array();

and then move your existing SortableDataObject::add_sortable_classes declaration below it, if you did already have one.

This will not only make the table now sort in the default_sort, but will remove the useful features of dataobject_manager from the table, so I’d generally advise not to do this; I’m simply showing that it can be done.