Archive for November, 2010

Php preg_match simple string examples

Due to ereg being deprecated in php 5.3.0 + I decided since it was at the core of one of the most heavily used functions on a site of mine, to make the move to preg_match this afternoon. More for my own benefit than anyone elses im just blogging it for future reference.

Some simple preg_matchs I wrote this evening to replace some old ereg functionality.

Firstly a simple match whole string of numeric and alphanumeric characters also including: space, (!), (_), (-), (|) and being between length of 1 and 200 characters. Like I said simples icon wink Php preg match simple string examples


if(!preg_match('/^[-A-Za-z0-9_!| ]{1,200}+$/',$data)){
return false;
} else {
return $data;
}

Heres another: This time only numeric characters are allowed, with a string length of 1-30.


if(!preg_match('/^[0-9]{1,30}+$/',$data)){
return false;
} else {
return $data;
}

Also like i said more for my own benefit than anyone elses, but feel free to comment if you’d like more examples.

Programming large scripts in javascript using object orientated style coding

This week whilst coding a large javascript / jQuery script for one of our online games which started to grow to an unmanageable size and needed to be compartmentalized I decided to review my techniques.

Previously for scripts which were quite large I would create an object which contained lots of different methods like so. As you should already know in js its all about scope.

So here’s what I used to do:

function page(){
this.funcName = function(param){
alert("hey man this is a cool little function);
}
}

I’d then go and call this function into the page like so.


var mypage = null;
function init(){
mypage = new page();
}

$(document).ready(function(){
init();
});

Which then gives you full access to call objects of page via access of the mypage var.

This works fine, but meant that all of my code was inside one very big ass object. Not so great. 2000 lines later, even less great. 2000 lines later and finding out I need to use aspects of it for one page and not another, awful.

So I set out to find a change in practise and this is what I came up with. This is not a new technique, nor do I personally claim I came up with it. Just my take on things I read.

My new plan was to create a base script, which was then interfaced by several smaller scripts dependant on the view.


Post={
newmsg: function(){
alert("hey there man, how goes it?");
}
};
pgAjax={
request: function(options){
//could add some extension of the defaults for a jquery ajax call here
}
}

Hopefully from the above you can start to see its benefits. By compartmentalising it, I made it much easier for me to keep code housed in segments, the objects can also be called via inline commands like onclick=”Posts.newmsg();”

I interface the base with a series of smaller files based on the view, similar to this.

leadview.js
$(function(){
$("input[type='button']").click(function(){
Posts.newmsg();
});
});

Obviously this is massively simplified, hopefully this might help though. Feel free to leave a comment if you wish for me to expand on this.

How to reselect a default option in a select box on the fly with jquery

How to reselect a option on the fly in a select element using jquery.

This always annoys me a little bit as I seem to have a tendancy to forget the solution, so I thought i’d just blog it this time round for future reference and hopefully it might also help someone else.

What I need it for quite often is setting a select box back to the default when using the select box to repopulate other elements on the page ie using the .change() function on the select box element.

Normally my solution for resetting it now is just this.

$("#elementID option[selected]").removeAttr("selected");

This will just set the focus back to the selected=”selected” element in the original html. Of course you could extend this somewhat further and perhaps actually set a new one if your list is not populated dynamically like so.

$("#elementID option[value='Today']").attr("selected","selected");

Hopefully this will be useful.

Removing and Replacing Apple iMac 8, (aluminum) hard drive.

First and foremost this tutorial covers from personal experience how to remove the outer shell of your iMac safely to get down to your main board.

Yesterday my hard drive in my iMac decided to fail on me for some beknown reason.

My symptoms and the apple support help page

Tried holding c during boot as suggested it might try and relocate the boot drive if it has lost track of its start up disc.
Secondly tried inserting the mac os x install disc and finding the hard drive in disk utitlity to repair.

If either of the above work for you, then you really don’t need to keep reading.

However in my case both of the above failed! My hard drive wasn’t even visible in the selectable list of drives recognized. (Bummer I thought).

My first thoughts were that if it were a normal computer i’d just open it up and see if it were some loose cabling or something visibly wrong with the hard drive. However as you should by now know, imacs are a all in one unit, meaning they are complexly put together and compact. Upon googling gaining access to the main board on an iMac, I immediately thought to myself do “woah this looks a bit complicated” and “maybe I shouldn’t do this”. Having now come out the other side i’m glad I didn’t let face complexity phase me.

I found this youtube video particuarly helpful, when looking into taking apart my iMac http://www.youtube.com/watch?v=2YsCTNVEYt8

In written format these are the steps that I took to gaining access to my main board and replacing the hard drive with a new Samsung f1 1tb 7200rpm drive (which was a bargain at only £43 from ebuyer) icon smile Removing and Replacing Apple iMac 8, (aluminum) hard drive. In replacing it, I traded it for a Western Digital 500gig drive that came with the iMac from new just over 18 months ago.

  1. First of all remove the ram slot at the bottom of the unit, turn it on its back and lay it flat on the desk, its only one screw and its pretty simply just a case of taking it out and removing the fascia it holds in.
  2. At this point you might also want to remove the ram modules for safe keeping, you can do this by first removing the black protective tape, then using two small flat head screw drivers and just wedging them either side of the module and levering it out gently.
  3. Next find a suction style pad, if you have dent pullers they will work, but we found the best size and most effective suction pad was actually for a car sat nav (my suggestion here is just get creative – any decent suction pad will do)
  4. Put the pad to the screen in the middle at either the left or right hand side it doesn’t matter, or if you wish you can use two, one on each side and do both at the same time.
  5. You will find the plastic screen comes off really easily, take it off gently and place it to one side.
  6. This will reveal the fascia screws, which are 8 torx screw size 8.
  7. Un-screw all of these, this will make the fascia loose and removeable, so go ahead and remove it, at the top you will find that the webcam lead will need disconnecting before removing.
  8. Next you’ll be wondering, do i really have to take the TFT out to get to the main board? The answer to this is yes you will!
  9. Its simple though so don’t worry too much. Another 8 screws and you will release the tft monitor, be careful when lifting this up though as there are 3 leads connected to it, it will help you to have someone else hold the TFT screen whilst you unplug it for care reasons here.
  10. Once you have un-screwed the TFT and un-plugged any cabling to it and made a mental note of what came from where, simply lift it up and put it somewhere safe.
  11. Now you will have access to the whole main board icon smile Removing and Replacing Apple iMac 8, (aluminum) hard drive. it should be fairly self explanatory as to whats what.

Removing the Hard Drive

  1. Identify the hard drive and remove the SATA cables and the thermometer.
  2. Handily apple have installed a nice easy to remove little catch, which you just pull down to release the drive, simply do that, remove the casing when detached from the mainboard and attach it to the new hard drive and simply replace it.

I’ve now attached some screenshots of the whole event.

20101102 eyqf11x6e931j55qx2e8ahfqxa.preview Removing and Replacing Apple iMac 8, (aluminum) hard drive.
Uploaded with Skitch!
20101102 gddimyxjssjgt5adx17ke7djqm.preview Removing and Replacing Apple iMac 8, (aluminum) hard drive.
Uploaded with Skitch!
20101102 tnb2f28eadeqe54fm7xsasjjm1.preview Removing and Replacing Apple iMac 8, (aluminum) hard drive.
Uploaded with Skitch!
20101102 b23i24g83wsbhb4j8rsf631b13.preview Removing and Replacing Apple iMac 8, (aluminum) hard drive.
Uploaded with Skitch!