My Twitter

Archive for the ‘Coding’ Category

URL Shortening Function

Friday, January 8th, 2010

Here is a function I use from time to time in my code which I thought I should share. It allows you to quickly shorten URL’s via 6 popular URL shortening websites.

function shorten_url($url, $service=NULL){
		if($service== 'tinyurl'){return get_link('http://tinyurl.com/api-create.php?url='.urlencode($url));}
		elseif($service == 'urly'){return get_link('http://ur.ly/new.txt?href='.urlencode($url));}
		elseif($service == 'isgd'){return get_link('http://is.gd/api.php?longurl='.urlencode($url));}
		elseif($service == 'klam'){return get_link('http://kl.am/api/shorten/?format=text&url='.urlencode($url));}
		elseif($service == 'unu'){return get_link('http://u.nu/unu-api-simple?url='.urlencode($url));}
		else{return get_link('http://api.tr.im/v1/trim_simple?url='.urlencode($url));}
		return $url;
	}

	function get_link($url){
		if(function_exists('curl_init')){
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_VERBOSE, 1);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			//curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
			//curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_URL, $url);
			$result = curl_exec($ch);
			$resultArray = curl_getinfo($ch);
			if ($resultArray['http_code'] == 200){
				return $result;
			} else {
				return FALSE;
			}
			curl_close($ch);
		}elseif(ini_get('allow_url_fopen') == 1){
			return fopen($url, 'r');
		}
		return FALSE;
	}

/* Usage */
echo shorten_url('http://www.fullondesign.co.uk/');
// returns: http://tr.im/JPCz

PHP Function: realpath()

Saturday, December 19th, 2009

One functions I wish I had known about when i start learned PHP is the realpath() function. Pretty much what it does is take symbolic link (such as ‘../’ or ‘/’) and returns a canonicalized absolute link (like ‘/home/public_html/folder’). Here is an example of it in action:

<?php
echo realpath('../');
// Would return: /home/mike/www/blogposts
?>

Google Chrome Frame – Good or Bad?

Wednesday, September 23rd, 2009

Recently Google Announced a new exciting product called Google Chrome Frame. Which is an add-on for Internet Explorer which offers Internet Explorer users all the features of other browsers without having to install a different browser (When a certain Meta tag is used on a web page)? The video Google released explains the product in a little more detail.

(more…)

Creating More User Friendly Forms with jQuery

Friday, September 11th, 2009

Create forms on a web page is relatively simple, however what most developers forget is the golden rule of usability, “Users are Idiots”.  In a large number of websites they fail to provide a clear indication of how a form field needs to be formatted. Some even seem to even forget to clear the field when the user clicks into, which is really useful if your website has users with motor skills.

Here is a really neat piece of jQuery I use to make forms a little easier for users which takes little work to integrate (Just copy and paste before the tag).

(more…)

An introduction to jQuery

Tuesday, August 18th, 2009

JQuery is a JavaScript Framework intends to decrease the time it takes to code the user enhancements; a website should always be able to work without JavaScript. JavaScript is just to improve the user experience.

Key Advantages of jQuery

  • Very easy to learn and code. If you are familiar with basic JavaScript and CSS you should be able to pick it up quickly.
  • A wide range of plugins available thanks to its large development community.
  • Its cross browser compatibility is great. The same code will be able to work in Firefox, Chrome, Opera, Safari or even IE6.
  • JQuery has fantastic documentation, which has a Firefox search engine plugin.

Unfortunately there is one disadvantage with jQuery. If a developer incorrectly utilizes it, the page loading times will drastically increase. This is mostly down to a developer adding lots of plugins without considering the total bandwidth required to send the data.

(more…)