Full On Design

Berkshire Based Web Development

 

Coding

How to use the Filter Functions in PHP

When I started learning PHP (Back in the PHP4 days) validating data was always a pain (for me at least). Most of the resources available cited the POSIX functions as the most effective way of validating an email address or URL.

Thankfully since then, the PHP community has embraced the PCRE functions which are more efficient and are Perl-compatible. However the downside to PCRE (and POSIX for that matter) is that you need to know regular expressions, which for a newbie to learn can feel like walking through a minefield.

Recently though the Filter Functions have become a very popular method to validate data. This is due to their small learning curve.

How to use the Filter Functions

In this example (Using the filter_var() function) the filter function takes the data you input (For example: email@example.com) and will return either the data (if it’s valid) or false (if the data is not valid).

Read the rest of this entry »

Apparently the new Google Chrome is fast…

Google Chrome is well known for being very fast at rendering web pages, but based on an update on the Google Student Blog it appears to be mind bogglingly snappy. To give you a sense of how snappy, take a look at this video comparing Chromes rendering to the speed to a potato.

Crazy Right? Well it’s all down to a new V8 JavaScript rendering engine, which boasts an improvement of 35%.

Read the rest of this entry »

URL Shortening Function

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()

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
?>